node:
Node v10
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.
Yes
I found a few problems with this issue:
Hi @peterpeterparker thanks for opening up the new issue. Can you provide a snippet of your functions code?
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);
}
}
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?
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.
@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
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.
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
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.
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
I ended up forking the firebase GitHub action repo and changing the container image to use the node 8 runtime
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.
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.
@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.
This worked for me as a workaround:
process.env.GCLOUD_PROJECT = JSON.parse(process.env.FIREBASE_CONFIG).projectId
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!
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.
Hi there @marcosflick this is due to another env var missing, X_GOOGLE_NEW_FUNCTION_SIGNATURE. It's the same issue as https://github.com/firebase/firebase-functions/issues/447.
Apologies for accidental duping! It was only related to this comment: https://github.com/firebase/firebase-functions/issues/437#issuecomment-498208619.
Internal tracking bug: 134416569
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",
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.
Node 10 functioning perfectly now :)
Any chance we could get a patch release for firebase-functions@2 as well? I'm stuck between this bug which seems to be broken in firebase-functions < 3, and this bug: https://github.com/firebase/firebase-tools/issues/1474 which is currently requiring me to stick to firebase-tools@6. firebase-functions@3 isn't compatible with firebase-tools@6. I guess my only choice is to downgrade to node 8 for now? :/
@keeganbrown unfortunately we don't do patch releases for old versions. Node 10 is only supported in functions >3, so your option would be to downgrade to 8 until the emulation bug gets fixed on firebase-tools.
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.
GCLOUD_PROJECT still undefined on google cloud functions.
I just started getting this error today. Been deploying latest versions yesterday but just now showed up :(. Had to revert to node 8, but I'm afraid I have dependencies that require 10.
Seeing the following error on logs during deployment
Warning, FIREBASE_CONFIG and GCLOUD_PROJECT environment variables are missing. Initializing firebase-admin will fail
& this error during runtime
Error: process.env.GCLOUD_PROJECT is not set.
at DocumentBuilder (/layers/google.nodejs.npm/npm/node_modules/firebase-functions/lib/providers/firestore.js:99:23)
at cloudFunction (/layers/google.nodejs.npm/npm/node_modules/firebase-functions/lib/cloud-functions.js:113:13)
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)
Targeting node 10.
Using firebase-tools v7.15.1 to deploy
Runtime Dependencies:
"@google-cloud/firestore": "3.7.1",
"firebase-admin": "8.10.0",
"firebase-functions": "3.3.0"
Seeing this error only with onWrite() trigger cloud function.
I have 2 other pubsub.schedule functions which work fine.
I am seeing this issue only when the cloud functions are deployed to us-east1.
Deploying to us-central1, the default region is working!!!!
Perhaps, someone from Google can check.
I got the same error since yesterday, however, I just updated to 3.4.0 where this issue is fixed.
https://github.com/firebase/firebase-functions/releases/tag/v3.4.0
it's the same issue as https://github.com/firebase/firebase-functions/issues/630
I got the same error since yesterday, however, I just updated to
3.4.0where this issue is fixed.https://github.com/firebase/firebase-functions/releases/tag/v3.4.0
it's the same issue as #630
Yes. It works with 3.5.0 Thanks @CsabaSzabo.
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.
@chrisjensen
engines field of package.json say? firebase --version. 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:
[email protected] & [email protected]
[email protected]
[email protected]
@laurenzlong
108.1.1 & 8.2.0I 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.
Hey all, I've been investigating the new reports of this, and I haven't been to reproduce it on [email protected] and [email protected]. If you experience this bug using these versions, please let us know. For now, if you are having trouble with this, try the workaround suggested in https://github.com/firebase/firebase-functions/issues/437#issuecomment-619834021
Hi @joehan, thanks, just upgraded to [email protected], deployed to europe-west1 and problem persists.
I don't see how this could be relevant to deploy machine, unless firebase deploy runs something like gcloud functions deploy ... --set-env-vars GCLOUD_PROJECT=... FIREBASE_CONFIG=... in the background, which would make these variables visible in General > Environment at https://console.cloud.google.com/functions/details/[region]/[function name]. Is that the intended behavior?
process.env after upgrading to [email protected] without https://github.com/firebase/firebase-functions/issues/437#issuecomment-619834021 workaround:
{
"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": "11",
"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"
}
Hi @jirizavadil thanks for the detailed report! Unfortunately we are still unable to reproduce it.
projectDirectory
functions
node_modules
package.json
(node_modules and package.json must both be at the root of the functions folder)
Add this to index.js
const functions = require("firebase-functions");
console.log(functions.config().foo.bar)
Run this in the CLI:
firebase functions:config:set foo.bar=faz
firebase deploy --only functions
Does the "faz" get printed in the functions logs?
Hey @laurenzlong
TLDR;
realized the env vars are only populated by/after require('firebase-functions') 🤦🏻♂️
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.
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)
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
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)
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 ?
@theochampion i delete all my functions and create again, works for me
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.
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-exactfirebase deploy --only functionsI updated to firebase-functions: 3.9.0 and it still doesn't work
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
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?
FIREBASE_CONFIG is always populated by the Firebase CLI when deploying, but will not be present when deploying via gcloud or other means.
Hi, I am upgrading my nodejs function form v8 to v10, I am getting the below 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 Promise.resolve.then (/layers/google.nodejs.functions-framework/functions-framework/node_modules/@google-cloud/functions-framework/build/src/invoker.js:198:28) at process._tickCallback (internal/process/next_tick.js:68:7) | more_vert
my node8 was working perfectly but this one occurs while upgrading can anyone help me with this one.
spec :
os: ubuntu 20.4.1
firebase cli : 8.6.0
firebase functions: 3.3.0
@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!)
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+.
The Node 10 environment does not include
GCLOUD_PROJECTanymore. The Firebase CLI automatically populatesFIREBASE_CONFIGwhich includes (when parsed as JSON) aprojectIdfield 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.
Most helpful comment
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.