Deploying Google Cloud Functions using private NPM modules

Viewed 425

When you deploy a Google Cloud Function, using the Node.js runtime, you can supply your code in a .zip file in Google Cloud Storage (--source=gs://...).

I make the zip file using all the compiled source, and the node_modules/ folder, so that GCF has everything it needs. I do this since I'm using private NPM modules, so npm i will not work inside the GCF build routine (that runs on cloud deploy inside GCP).

Problem: Even if I include 100% of the needed modules in node_modules/, GCF still has deployment errors, since it tries to access the private NPM package repo.

If I remove the package.json's dependencies and devDependencies section, GCF will fail to start, saying it cannot find the modules.

So:

  1. Why can't GCF just use bundled dependencies like other tools do (e.g. GitHub Actions)
  2. What is the best practice for deploying GCFs using private NPM modules?
2 Answers

the best way is to include in your code a file called .npmrc, in your npm account can generate an access token with some permissions and that token is configured in the file that I mentioned above, in that way when the deploy is made, the command npm install can download the private packages.

Related