10

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 Cloud Functions.

What's the best way to write a library of shared code and have that accessed by multiple Cloud Functions.

For example, say I have two Cloud Functions, functionA and functionB.

I have a node javascript file called "common.js" that has a javascript function that I'd like to expose to both functionA and functionB.

exports.common = {
    log: function(message) {
        console.log('COMMON: ' + message);
    }
};

So in functionA I'd like to require this file and call "common.log('test');".

I see this as the most basic of questions but I honestly can't find an answer anywhere.

Any help would be most appreciated. This is literally the ONLY thing preventing me from using GCF as the way I develop code now and into the future!

Encoder
  • 525
  • 1
  • 4
  • 15

2 Answers2

9

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)

Community
  • 1
  • 1
Bret McGowen
  • 1,270
  • 11
  • 8
  • Wow... I just didn't have the right frame of mind when it came to functions. It's so bloody obvious now! I've got my set up correct and it's working 100% perfectly. THANK YOU! – Encoder Mar 04 '17 at 11:07
  • 3
    Thanks for the reply. If my understanding is correct, this implies that if you have 3 functions you want to deploy and each is using the same common lib you have to copy this common lib in each function folder? Isn't there a way to share this lib among the 3 functions without copying it ? Thanks! – fro Aug 22 '18 at 09:57
  • 4
    What if my `lib/common.js` is located **OUTSIDE** the `functions` folder? Will it work? – Antonio Ooi Dec 20 '20 at 10:33
1

The best method I've found for dealing with this (without having to publish your package through npm) is to use a "pre-deploy" copy script, ie:

"scripts": {
    "copy-shared": "rm -rf ./shared && cp -rf ../shared ./ && cp ../credentials.json ./",
    "deploy": "npm run copy-shared && gcloud functions deploy updateUser --runtime nodejs12 --trigger-http",
    "dev": "npm run copy-shared && IS_DEV=true functions-framework --target=updateUser"
}

So you'd just run 'npm run dev' (for local testing) or 'npm run deploy' (for publishing), and it will update the shared folder with one outside the current directory structure.

Ryan Weiss
  • 1,308
  • 1
  • 14
  • 36