firebase-functions: GCLOUD_PROJECT environment variable missing from the Node 10 runtime

[REQUIRED] Version info

node: Node v10

[REQUIRED] Steps to reproduce

I have upgraded my functions from Node v8 to Node v10. Compilation as upload in the cloud are ok but I face an error at runtime, respectively GCLOUD_PROJECT is not found/set

Here the stack trace of the error I found in the Firebase console:

Error: process.env.GCLOUD_PROJECT is not set.
    at DocumentBuilder (/srv/functions/node_modules/firebase-functions/lib/providers/firestore.js:99:23)
    at cloudFunctionNewSignature (/srv/functions/node_modules/firebase-functions/lib/cloud-functions.js:102:13)
    at cloudFunction (/srv/functions/node_modules/firebase-functions/lib/cloud-functions.js:151:20)
    at Promise.resolve.then (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28)
    at process._tickCallback (internal/process/next_tick.js:68:7)

When I downgrade back to Node v8, I do not face that error.

Were you able to successfully deploy your functions?

Yes

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 46
  • Comments: 68 (19 by maintainers)

Commits related to this issue

Most upvoted comments

The issue should be fixed in the latest release if you update to 3.0.1. Will close this out. Thanks all for your patience.

I get this error in 3.3.0.

I have a test setup, where I extracted the project initialization into a separate file. When I run one test, it works, when I run two tests, I get this error.

This worked for me as a workaround:

process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId

Ah yes, this confirms it then. Thank you for sharing your code! This is definitely a bug on our side with Node 10. I will make a note of this and try to get a fix out - I’ll need to think on the best way to implement it though, so that might take a bit of time. The absence of this env variable impacts most event source providers, not just Firestore like in your case, so we need to be careful on how we go about fixing this. In the mean time, please use Node 8. I’ll make sure to post any updates to this thread periodically.

Quick update: Node 10 runtime is the first runtime to not have the GCLOUD_PROJECT environment variable automatically set. Because the firebase functions SDK relies on this env var for most of its event provider code, we need to figure out the best way to get this GCLOUD_PROJECT value set. Meanwhile, please continue using Node 8 until this issue is fixed.

Ah, @wvanderdeijl thanks for that - this should indeed work! Anyone experiencing this issue - please use the workaround provided in https://github.com/firebase/firebase-functions/issues/437#issuecomment-497626133 or downgrade to Node 8. We are currently working on a fix on our end and I’ll keep this issue open until that fix is rolled out. Thanks for everyone’s patience!

Node 10 runtime (which is currently in beta) no longer automatically sets project related environment variables, so I would expect process.env.GCP_PROJECT to not return anything. As a workaround until we fix this please use Node 8 runtime.

  • Hardcode (1st line before loading/importing anything else) your project ID: process.env.GCLOUD_PROJECT = “your-project-id”; (this worked for me, other options are not tested)
  • Derive from your own env variable Even tho, it’s not the cleanest solution messing with env variables inside the source code (for tests it’s a different story), it might be a better alternative than falling back to node 8.

For anyone still having this issue, the solution by @wvanderdeijl no longer works, but I was able to figure another one out, based on it.

process.env.GCLOUD_PROJECT = (req, res) => { res.send(JSON.parse(process.env.FIREBASE_FUNCTION).projectId); };

Hey @laurenzlong

TLDR; realized the env vars are only populated by/after require('firebase-functions') 🤦🏻‍♂️


  1. same
  2. yep, this is where you made me realize the require('firebase-functions') is necessary.

I’m coming from Google Cloud Functions, where (custom) environment variables are ‘real’ - living in the OS, not injected on runtime. So I didn’t realize the mechanism. Sorry for wasting your time, however a little mention in the docs on how this and the deploy works behind the scenes shouldn’t hurt anyone 🙃. There are so many differences between these similar products with a similar name and no centralized place that would explain it.

Same, I have a cloud function that’s been in use for 6 months, was working fine, suddenly yesterday I was unable to deploy because the environment variable is missing. Quite concerning to see that such a change that can affect all deploys just suddenly crops up in production deploys.

GCLOUD_PROJECT still undefined on google cloud functions.

Node 10 functioning perfectly now 😃

i’ve deployed all my functions with NodeJS 10 today, and I see the same issue, but only in functions triggered by Firestore record creation, like:

module.exports = functions.firestore.document('collection/{recordId}').onCreate((snap, context) => {

Functions triggered by HTTP work fine and return data without issues

stackdriver logs are:

Error: process.env.GCLOUD_PROJECT is not set.

        at DocumentBuilder (/srv/functions/node_modules/firebase-functions/lib/providers/firestore.js:99)
        at cloudFunctionNewSignature (/srv/functions/node_modules/firebase-functions/lib/cloud-functions.js:102)
        at cloudFunction (/srv/functions/node_modules/firebase-functions/lib/cloud-functions.js:151)
        at Promise.resolve.then (invoker.js:330)
        at process._tickCallback (next_tick.js:68)

package.json:

    "@google-cloud/firestore": "^2.2.0",
    "firebase-admin": "^8.1.0",
    "firebase-functions": "^2.3.1",

I ended up forking the firebase GitHub action repo and changing the container image to use the node 8 runtime

Glad you figured it out! Yes we are working on injecting it in the OS, not just on runtime (https://github.com/firebase/firebase-tools/pull/2186)

Hi, got the same issue. My workaround (extending @pavadeli’s solution https://github.com/firebase/firebase-functions/issues/630#issuecomment-600259931 in https://github.com/firebase/firebase-functions/issues/630):

if (!process.env.FUNCTIONS_EMULATOR) {
  const { readFileSync } = require('fs')
  const { resolve } = require('path')
  try {
      process.env.CLOUD_RUNTIME_CONFIG = process.env.CLOUD_RUNTIME_CONFIG || readFileSync(resolve(__dirname, '.runtimeconfig.json'), 'utf8')
      let cloudRuntimeConfig = JSON.parse(process.env.CLOUD_RUNTIME_CONFIG)
      process.env.GCLOUD_PROJECT = process.env.GCLOUD_PROJECT || cloudRuntimeConfig.firebase.projectId
      process.env.FIREBASE_CONFIG = process.env.FIREBASE_CONFIG || JSON.stringify(cloudRuntimeConfig.firebase)
      
  } catch (e) {
      console.log('setting CLOUD_RUNTIME_CONFIG failed', e)
  }
}

process.env before:

{
  "NO_UPDATE_NOTIFIER": "true",
  "FUNCTION_TARGET": "events.sites.***",
  "NODE_OPTIONS": "--max-old-space-size=512",
  "NODE_ENV": "production",
  "PWD": "/srv",
  "HOME": "/root",
  "DEBIAN_FRONTEND": "noninteractive",
  "PORT": "8080",
  "K_REVISION": "9",
  "K_SERVICE": "events-sites-***",
  "SHLVL": "1",
  "FUNCTION_SIGNATURE_TYPE": "event",
  "PATH": "node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  "_": "/layers/google.nodejs.functions-framework/functions-framework/node_modules/.bin/functions-framework"
}

process.env after:

{
  "NO_UPDATE_NOTIFIER": "true",
  "FUNCTION_TARGET": "events.sites.***",
  "NODE_OPTIONS": "--max-old-space-size=512",
  "NODE_ENV": "production",
  "PWD": "/srv",
  "HOME": "/root",
  "DEBIAN_FRONTEND": "noninteractive",
  "PORT": "8080",
  "K_REVISION": "10",
  "K_SERVICE": "events-sites-***",
  "SHLVL": "1",
  "FUNCTION_SIGNATURE_TYPE": "event",
  "PATH": "node_modules/.bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
  "_": "/layers/google.nodejs.functions-framework/functions-framework/node_modules/.bin/functions-framework",
  "CLOUD_RUNTIME_CONFIG": "{\n \"firebase\": {\n \"projectId\": \"***-development\",\n \"databaseURL\": \"https://***-development.firebaseio.com\",\n \"storageBucket\": \"***-development.appspot.com\",\n \"locationId\": \"europe-west\"\n }\n}",
  "GCLOUD_PROJECT": "***-development",
  "FIREBASE_CONFIG": "{\"projectId\":\"***-development\",\"databaseURL\":\"https://***-development.firebaseio.com\",\"storageBucket\":\"***-development.appspot.com\",\"locationId\":\"europe-west\"}"
}

versions: firebase-tools@8.1.1 & firebase-tools@8.2.0 firebase-functions@3.6.1 node@10.16.2


@laurenzlong

  1. 10
  2. 8.1.1 & 8.2.0
  3. n/a (my functions are new, i was migrating everything from Google Cloud Functions, where i deployed via gcloud)

I have tried deploys without region, with us-central1 & europe-west1, from two different localhosts and from a docker image in Cloud Build, with no difference in result.

Hi everybody!

We’ve attempted all the solutions proposed in this thread and it hasn’t resolved this issue.

Code:

// the function doesn't deploy when this is in the first line:
// process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId
const functions = require('firebase-functions')

// either lines below deploy
// process.env.GCLOUD_PROJECT = "ms-threat-research"
process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId

exports = module.exports = functions.firestore
  .document('cases/{caseId}/artefacts/{artefactId}')
  .onCreate(async (snap, context) => {
    console.log('test')
  })

Error:

TypeError: Cannot read property 'name' of undefined
    at snapshotConstructor (/srv/functions/node_modules/firebase-functions/lib/providers/firestore.js:125:72)
    at cloudFunctionNewSignature (/srv/functions/node_modules/firebase-functions/lib/cloud-functions.js:119:34)
    at cloudFunction (/srv/functions/node_modules/firebase-functions/lib/cloud-functions.js:151:20)
    at Promise.resolve.then (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:330:28)
    at process._tickCallback (internal/process/next_tick.js:68:7)

Could you please propose a fix for all this?

Thank you in advance.