Jimmy
Jimmy

Reputation: 12497

Trying to deploy my first google cloud function - express not found

I’m trying to deploy my first google cloud function, the code of which is here:

https://github.com/rldaulton/GCF-Stripe/blob/master/Charge%20Customer/index.js

It starts with this line:

var app = require('express')();
var http = require('http').Server(app);

However I get this error when I try and deploy with Node6 and execute function name chargeCustomer:

Function failed on loading user code. Error message: Code in file index.js can't be loaded.
Did you list all required modules in the package.json dependencies?
Detailed stack trace: Error: Cannot find module 'express'
    at Function.Module._resolveFilename (module.js:476:15)
    at Function.Module._load (module.js:424:25)
    at Module.require (module.js:504:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (/user_code/index.js:1:91)
    at Module._compile (module.js:577:32)
    at Object.Module._extensions..js (module.js:586:10)
    at Module.load (module.js:494:32)
    at tryModuleLoad (module.js:453:12)
    at Function.Module._load (module.js:445:3)

It seems like I’m missing module express. How do I add this?

Upvotes: 0

Views: 966

Answers (1)

Kolban
Kolban

Reputation: 15266

When you deploy a Node.js code function with Cloud Functions the responsibility is on you to also supply a well formed package.json file which declares the dependencies your function needs which usually match the requires() statements within your code. In your example, your code requires express so you would have to supply a package.json that declares that you require express. For example:

{
  "dependencies": {
    "express": "^4.16.4"
  }
}

Here is a link to the documentation at Google relating to dependencies for Node.js for Cloud Functions.

Upvotes: 3

Related Questions