Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCLOUD_PROJECT environment variable missing from the Node 10 runtime #437

Closed
peterpeterparker opened this issue May 10, 2019 · 68 comments
Closed
Assignees
Labels
node-10 tracked internally Issues that are tracked internally, even though the issue is closed type: bug

Comments

@peterpeterparker
Copy link

peterpeterparker commented May 10, 2019

[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

@google-oss-bot
Copy link
Collaborator

I found a few problems with this issue:

  • I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.
  • This issue does not seem to follow the issue template. Make sure you provide all the required information.

@thechenky
Copy link
Contributor

Hi @peterpeterparker thanks for opening up the new issue. Can you provide a snippet of your functions code?

@thechenky thechenky self-assigned this May 10, 2019
@peterpeterparker
Copy link
Author

peterpeterparker commented May 10, 2019

sure @thechenky here you go:

import {CollectionReference, DocumentSnapshot, QuerySnapshot, DocumentReference} from '@google-cloud/firestore';

import * as admin from 'firebase-admin';

import {Change, EventContext} from 'firebase-functions';

export async function applyUsersUpdate(change: Change<DocumentSnapshot>, context: EventContext) {
    const newValue: any = change.after.data() as any;

    const previousValue: any = change.before.data() as any;

    if (!newValue || !previousValue || newValue.my_field === previousValue.my_field) {
        return;
    }

    try {
        await myUpdateMethod(change, newValue);
    } catch (err) {
        console.error(err);
    }
}

@thechenky
Copy link
Contributor

Hm, I would expect to see something calling Firestore to set the value (since the error is coming from here: firebase-functions/lib/providers/firestore.js:99:23). Is that in the myUpdateMethod? Would you mind pasting that too?

@thechenky
Copy link
Contributor

This is almost certainly due to GCLOUD_PROJECT env var no longer being populated in the Node 10 runtime. Are you deploying your functions using the Firebase CLI? I'm thinking maybe we can set that environment when we deploy your functions as a stopgap.

@MaximBazarov
Copy link

MaximBazarov commented May 10, 2019

@thechenky in my case it's

exports.UpdateCountriesValues = functions.firestore
    .document(COUNTRIES_COLLECTION + '/{needsValues}')
    .onWrite(async (change, context) => {
        try {
            const id = await getID(change);
            return updateValues(id, COUNTRIES_COLLECTION);
        }
        catch (e) {
            return e;
        }
    });


async function updateValues(id, collection) {
    const token = await authRequest();
    const values = await makeRequest(valuesRequest(token, id));
    const batch = db.batch();
    let years = [];
    values.forEach((value) => {
        years.push(value.year.toString());
        const collectionName = collection + "-values";
        let record = Values(value);
        const valueRef = db
            .collection(collectionName)
            .doc(id.toString() + "-" + value.year.toString());
        batch.set(valueRef, record);
        if (collection === COUNTRIES_COLLECTION) {
            const countryCapitalRef = db
                .collection(COUNTRIES_COLLECTION)
                .doc(id.toString());
            batch.set(countryCapitalRef, {
                en: {
                    capital: value.report_type_eng,
                },
                ru: {
                    capital: value.report_type,
                }
            }, { merge: true });
        }
    });
    years.forEach((year) => {
        const yearRef = db
            .collection(YEARS_COLLECTION)
            .doc(year.toString());
        batch.set(yearRef, { year: year });
    });
    const objectRef = db
        .collection(collection)
        .doc(id.toString());
    batch.set(objectRef, {
        "needsValues": false,
    }, { "merge": true });
    await batch.commit();
    return id;
}

I'm deploying using the Firebase CLI

@thechenky
Copy link
Contributor

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.

@peterpeterparker
Copy link
Author

Thx @MaximBazarov for the code, you were faster than me 😉

@thechenky thx for having a look at the problem and if you still need the example of code from my side or a tester, for sure let me know

@thechenky
Copy link
Contributor

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.

@kdawgwilk
Copy link

Is there a different workaround for those that need to use Node 10 runtime? I am using GitHub Actions to auto deploy my projects changes on push and the GitHub NPM action uses a Node 10 container and so if I drop the "engines" to 8 it breaks my CI flow

@crenardy
Copy link

crenardy commented May 27, 2019

  • 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.

@kdawgwilk
Copy link

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

@gregghz
Copy link

gregghz commented May 29, 2019

i set process.env.GCLOUD_PROJECT = process.env.GCP_PROJECT at the top of my file and then the firebase function failed to deploy with an http 400 response.

@thechenky
Copy link
Contributor

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.

@thechenky
Copy link
Contributor

@gregghz your deployments should not fail because environment variables are missing. If you're still experiencing this issue please file a new bug and fill out all the required fields. Error 400 is not enough information to debug what may be happening.

@wvanderdeijl
Copy link

This worked for me as a workaround:

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

@thechenky
Copy link
Contributor

Ah, @wvanderdeijl thanks for that - this should indeed work! Anyone experiencing this issue - please use the workaround provided in #437 (comment) 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!

@marcosflick
Copy link

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.

@thechenky
Copy link
Contributor

thechenky commented Jun 3, 2019

Hi there @marcosflick this is due to another env var missing, X_GOOGLE_NEW_FUNCTION_SIGNATURE. It's the same issue as #447.

@thechenky thechenky marked this as a duplicate and then as not a duplicate of #447 Jun 3, 2019
@thechenky
Copy link
Contributor

Apologies for accidental duping! It was only related to this comment: #437 (comment).

@thechenky
Copy link
Contributor

Internal tracking bug: 134416569

@tettoffensive
Copy link

I was hoping to perform firestore actions in the node REPL

node --experimental-repl-await

But was seeing this:

Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail`

I'm running Node v10.19.0, firebase-tools v8.2.0, firebase-functions v3.6.1

@ffacal
Copy link

ffacal commented Jun 17, 2020

Hi @laurenzlong I'm experiencing this issue again, running a firestore onCreate listener (nodeJS 10 runtime).

Updated my firebase-tools to 8.4.2 but still gettng:

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

@tzvc
Copy link

tzvc commented Jun 18, 2020

Experiencing the problem on Node v10.16.0 when unit testing my functions

firebase-functions-test : "^0.2.1"
firebase-functions: "^3.7.0"

     Error: process.env.GCLOUD_PROJECT is not set.
      at DocumentBuilder (node_modules/firebase-functions/lib/providers/firestore.js:100:23)
      at Function.get (node_modules/firebase-functions/lib/cloud-functions.js:149:17)
      at hasPath (node_modules/lodash/lodash.js:6131:24)
      at Function.has (node_modules/lodash/lodash.js:13159:32)
      at Object.wrap (node_modules/firebase-functions-test/lib/main.js:28:19)
      at triggerPostWrite (test/unit/db/posts/onCreate.spec.ts:40:26)
      at Context.it (test/unit/db/posts/onCreate.spec.ts:77:35)

Any updates on this ?

@svhen
Copy link

svhen commented Jun 19, 2020

@theochampion i delete all my functions and create again, works for me

@CyrusZei
Copy link

for anybody that gets some strange errors like functions.auth.user().onCreate((user) => console.log('user',user ) == undefiend

This is what I did to fix it.

  1. First, upgrade your nodejs 8 to nodejs 10
  2. upgrade your cloud functions to the latest version (this is the cause of all the strange errors) so cd into your functions folders and then run this command to update it npm install firebase-functions@latest --save and if you need you can update the firebase-admin so also run this npm install firebase-admin@latest --save-exact
  3. deploy your functions by running this command firebase deploy --only functions
  4. Watch everything works

@roger-ngx
Copy link

I updated to firebase-functions: 3.9.0 and it still doesn't work

@StephanKornerTrihow
Copy link

Same here, does not work - I keep getting the VSConsole message:

Warning, estimating Firebase Config based on GCLOUD_PROJECT. Initializing firebase-admin may fail

What does this mean ?

I am on

  • node 10.22.0
  • using functions-emulator

@wujekbogdan
Copy link

wujekbogdan commented Sep 30, 2020

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

How reliable this workaround is? I'm also doing the same but FIREBASE_CONFIG does not appear on the officially supported node10 environment variables.

So it makes me wonder whether is it just an internal Firebase variable that may change in the future or is it just a bug in the documentation and the variable should be listed there?

@mbleigh
Copy link
Contributor

mbleigh commented Sep 30, 2020

FIREBASE_CONFIG is always populated by the Firebase CLI when deploying, but will not be present when deploying via gcloud or other means.

@reuben-bradley
Copy link

reuben-bradley commented Nov 11, 2020

@kushal-iotasol
Ran into this issue while upgrading from Node v8 to v10 as well.

There's a workaround given by @wvanderdeijl in their comment further up in the thread, but this didn't seem "permanent" enough for my liking.

Here's what worked for me: delete your functions, and then re-deploy them. For some reason, simply re-deploying Node 10 functions over existing Node 8 functions doesn't appear to set the environment variables correctly, whereas deleting them first seems to do the trick!
(Cheers to @svhen for the idea!)

@mbleigh
Copy link
Contributor

mbleigh commented Nov 12, 2020

The Node 10 environment does not include GCLOUD_PROJECT anymore. The Firebase CLI automatically populates FIREBASE_CONFIG which includes (when parsed as JSON) a projectId field that is equivalent. This is the recommended way to access the project ID in Node 10+.

@antoniooi
Copy link

The Node 10 environment does not include GCLOUD_PROJECT anymore. The Firebase CLI automatically populates FIREBASE_CONFIG which includes (when parsed as JSON) a projectId field that is equivalent. This is the recommended way to access the project ID in Node 10+.

Nope, even the process.env.FIREBASE_CONFIG is missing:
SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)

Although hardcoding the process.env["GCLOUD_PROJECT"] with project id works, I doubt that the process.env.FIREBASE_CONFIG contains any information from the remoteconfig.json. Solution by @jirizavadil may work but not yet tested.

@sceee
Copy link

sceee commented Dec 17, 2020

@antoniooi using it like this works for me:

const firebaseConfig = process.env.FIREBASE_CONFIG
if (firebaseConfig === undefined) {
  throw new Error('Firebase Config is undefined')
}

const adminConfig: {
  databaseURL: string
  storageBucket: string
  projectId: string
} = JSON.parse(firebaseConfig)
const PROJECT_ID = adminConfig.projectId

@DamoVigo
Copy link

DamoVigo commented Jan 7, 2021

hello ! I just tried and still not working for me..

@p2lvoizin
Copy link

Hello,

I got the same issue.
On my local environnement exporting the env var fix the issue.

export GOOGLE_APPLICATION_CREDENTIALS="boo"

But when uploading my function I got the issue back even if export the variable while uploading my function.
For my case exporting the en var from a yaml file fix the issue.

So try setting env vars from yaml file like that :

gcloud functions deploy FUNCTION_NAME --build-env-vars-file FILE_NAME.yaml FLAGS...

yaml file :
GCLOUD_PROJECT: boo

Even a wrong value works for me but you could met some issue.
spec :

  • nodejs12
  • firebase-functions 3.13.1

@DamoVigo
Copy link

DamoVigo commented Feb 15, 2021 via email

@julianodportela
Copy link

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

@TeoSlayer
Copy link

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

This worked for me

@ucaboro
Copy link

ucaboro commented Mar 13, 2021

I'll just add here what worked for me since by the looks of it some can still experience problems when using node 10+

Update your firebase-admin and firebase-functions, run npm i, re-deploy your functions.

snippet from package.json

...
"engines": {
    "node": "12"
  },
  "dependencies": {
    "firebase-admin": "^9.4.2",
    "firebase-functions": "^3.13.1"
  }
...

With the above approach I didn't need to use process.env.GCLOUD_PROJECT solution provided above.

@jlin5
Copy link

jlin5 commented Jan 12, 2022

For anyone seeing this warning in the logs when the cloud function is deployed without using the Firebase CLI, I resolved it by adding a .runtimeconfig.json file with these properties:

{
  "firebase": {
    "projectId": "$projectId"
  }
}

You would need to replace $projectId with your own project's id.

Note: I only tested this on initialization of the Firebase Admin SDK with no arguments.

const admin = require('firebase-admin');
admin.initializeApp();

Thanks to @mbleigh for the clarification!

The Node 10 environment does not include GCLOUD_PROJECT anymore. The Firebase CLI automatically populates FIREBASE_CONFIG which includes (when parsed as JSON) a projectId field that is equivalent. This is the recommended way to access the project ID in Node 10+.

Related links: https://firebase.google.com/docs/functions/config-env#automatically_populated_environment_variables

@wujekbogdan
Copy link

wujekbogdan commented Jan 13, 2022

@jlin5

It isn't a solution if you have more environments (dev, staging, production) where each environment has its own id. If we hardcode the project name in the config file it's no different from hardcoding the project id in the source code of the application.

@jlin5
Copy link

jlin5 commented Jan 13, 2022

@wujekbogdan

That's right. This is to resolve the warning itself. A solution for multiple environments that I can think of is to generate the .runtimeconfig.json in your deployment scripts and using gcloud config get-value project to get the project id instead of hardcoding it.

@chenqiuxia
Copy link

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

not working for me

@thecaptainXgod
Copy link

Any development here?

@sepisoltani
Copy link

anyone found the right solution to shut up the warnings?

@keeganbrown
Copy link

If you're still getting warnings here, theres a good chance you need to reinstall the firebase-tools cli and update firebase within your project --

npm install firebase-functions@latest firebase-admin@latest --save
npm install -g firebase-tools

@kurt-s
Copy link

kurt-s commented Sep 6, 2023

@thatfiredev I upgraded these tools:

npm install firebase-functions@latest firebase-admin@latest --save  
npm install -g firebase-tools

My package.json (inside the "functions" folder):

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase emulators:start --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "engines": {
    "node": "18"
  },
  "main": "index.js",
  "dependencies": {
    "axios": "^1.4.0",
    "firebase-admin": "^11.10.1",
    "firebase-functions": "^4.4.1"
  },
  "devDependencies": {
    "eslint": "^8.15.0",
    "eslint-config-google": "^0.14.0",
    "firebase-functions-test": "^3.1.0"
  },
  "private": true
}

My package.json (one level above the "functions" folder):

{
  "dependencies": {
    "@firebase/app": "^0.9.17",
    "axios": "^1.5.0",
    "firebase-admin": "^11.10.1",
    "firebase-functions": "^4.4.1"
  }
}

Part of my index.js code:

const functions = require("firebase-functions");
const {logger} = require("firebase-functions");
const admin = require("firebase-admin");

admin.initializeApp();

...

exports.myFunction = functions.firestore
  .document("myCollection/{id}")
  .onCreate(async (snap, context) => {
...

Then deployed my function:

gcloud functions deploy myFunction --entry-point=myFunction--timeout=300 --trigger-event providers/cloud.firestore/eventTypes/document.create --runtime=nodejs18 --max-instances=1 --trigger-resource "projects/<id>/databases/(default)/documents/myCollection/{id}" --docker-registry=artifact-registry

I still get the Error: process.env.GCLOUD_PROJECT is not set. error in the logs when executing the fuction. Plus: now I also don't see any of my logging output in the cloud logs. Not sure if this is related to this projectId issue...

I also tried to add this line before initializing the firebase-admin SDK like so:


process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId;
admin.initializeApp();

But this leads to an error when trying to deploy the function to the cloud:

Deploying function (may take a while - up to 2 minutes)...failed.
ERROR: (gcloud.functions.deploy) OperationError: code=3, message=Function failed on loading user code. This is likely due to a bug in the user code. Error message: Provided module can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: SyntaxError: Unexpected token u in JSON at position 0

I also set the projectId like so:

admin.initializeApp({
  projectId: "myprojectid",
});

But that also didn't help.

I also tried setting it within both .runtimeconfig.json files as suggested by @jlin5 but that also didn't help.

The only thing that did help? I deleted the function from Firestore and re-deployed it. Yes, after many hours of researching and testing, that was the solution for me. I can't tell you how frustrating this was...now also my logs work properly.

@ydax
Copy link

ydax commented Sep 20, 2023

I'm having this issue. Been working on it all day. So, it's still an issue in September of 2023.

Okay, so, for what it's worth, our app was running these dependency versions when we were getting this error:

"firebase-admin": "^11.10.1",
"firebase-auth": "^0.1.2",
"firebase-functions": "^4.4.1",
"firebase-tools": "9.3",

I downgraded them to these:

"firebase-admin": "^9.11.0",
"firebase-auth": "^0.1.2",
"firebase-functions": "^3.16.0",
"firebase-tools": "9.3",

...then I redeployed our function and it worked.

As additional context, all of our other onCall and onRequest Cloud Functions were working fine with the first set of dependencies, it was only when we added an onWrite function that we saw this error (and only in the onWrite function).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
node-10 tracked internally Issues that are tracked internally, even though the issue is closed type: bug
Projects
None yet
Development

No branches or pull requests