I\'m looking to write a custom library in node and I\'d like to include that with my Cloud Functions. Since this is shared code, I\'d like to be able to use it across all my Clo
If you use the gcloud command line tool to deploy your function it will upload all1 the files in your local directory, so any normal Node.js way of doing an include/require should work.
In Node.js, writing require('./lib/common')
will include the common.js
file in the lib
subdirectory. Since your file exports an object named common
you can reference it directly off the returned object from require
. See below.
./
../
index.js
lib/common.js
// common.js exports a 'common' object, so reference that directly.
var common = require('./lib/common').common;
exports.helloWorld = function helloWorld(req, res) {
common.log('An HTTP request has been made!');
res.status(200);
}
$ gcloud functions deploy helloWorld --trigger-http
Note
1 Currently gcloud
won't upload npm_modules/
directory unless you specify --include-ignored-files
(see gcloud docs)