Google Cloud Functions include private library

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.

File layout

./
../
index.js
lib/common.js

index.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);
}

Deploy

$ 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)