Search code examples
javascriptrequiregoogle-cloud-functionspackage.json

Google cloud functions :: cant use 'require' statements


Issue

When I include any 'require' statements in a google cloud function, I get the warning: "Function is active, but the last deploy failed"

Solution ?

I'm pretty sure I need to include dependencies in the package.json file, but I don't know what dependencies to include, or how to write that.

Background

I've built an android app in Java and I'm trying to integrate stripe payments. Stripe requires me to have a server handle the requests, but I'm trying to use google cloud functions instead (so I don't have to pay / manage a server).

I'm trying to follow this example, but it's not working. The author didn't include the package.json file and I'm not sure what dependencies to put in there. I've never written java script before, my background is in python, c++, java.

I've looked at this tutorial from google as well as the google docs on writing cloud functions. I've also searched S.O. and can't find a solution. The problem may be that I'm not a javascript developer. I'm trying to just plug and play someone else's code to make one specific part of my android (java) app work.

Troubleshooting

To troubleshoot, I used the "helloWorld" example provided by google. The Hello World function works find by itself. If I add any of these three require statements at the top, I get the warning: "Function is active, but the last deploy failed"

Code

-- index.js

var app = require('express')();
var http = require('http').Server(app);
var stripe = require('stripe')(
  "your_stripe_key"
);
var bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

//initiate a one-off charge for a customer
exports.chargeCustomer = app.get(".../charge/:customerid/:token/:amount", function chargeCustomer (req,res){
  stripe.charges.create({
    customer: req.params.customerid,
    source: req.params.token,
    currency: 'usd',
    amount:req.params.amount
  },function(err, charge) {
    if(err) {
      return res.send(JSON.stringify(err));
    }
    res.send(JSON.stringify(charge));
  });
});

-- package.json

{
  "name": "sample-http",
  "version": "0.0.1"
}

Notes

If you know what I'm doing wrong, please remember I've never written javascript. I don't think I'm asking a duplicate question, I've searched, but its possible the answer is in another question and I'm just not understanding it because I've never written javascript.


Solution

  • I wrote the repo that you referenced in your question above. The problem is that you are not formatting your package.json file correctly.

    When you deploy your functions, you will need to deploy your index file and a corresponding package.json. You can deploy either through command line, or by simply using the Google in-line editor in the Cloud Functions product.

    The package.json file specifies the dependencies that your code needs to run. Basically, your index file needs to know which 3rd party libraries to require for it's functionality. You will notice in my example code below includes a small node for "dependencies", which will tell Google Cloud Functions which packages to install with your code.


    The specific index file you reference is creating a charge via Stripe. I use a variation of this code in many production products, and the package.json file looks like this:

    {
      "name": "createCharge",
      "version": "1.0.0",
      "description": "create and charge in Stripe",
      "main": "index.js",
      "scripts": {
        "test": "echo \"Error: no test specified\" && exit 1"
      },
      "keywords": [],
      "author": "Your Name",
      "license": "ISC",
      "dependencies": {
        "express": "^4.14.0",
        "stripe": "^4.4.0",
        "body-parser": "~1.13.3",
        "async": "^2.0.1",
        "promise": "^7.1.1"
      },
      "engines": {
        "node": "4.1.1"
      },
      "repository": {
        "type": "git",
        "url": "<path/to/your/repo.git>"
      }
    }
    

    Also, just so that you are aware, the "repository" chunk of the above javascript is not required, so if you don't have a hosted repo for this specific function, feel free to delete it.

    Hope this helps!