Firebase-functions: FIREBASE_CONFIG not set when Node.js 10 function deployed

Created on 4 May 2019  路  25Comments  路  Source: firebase/firebase-functions

[REQUIRED] Environment info


firebase-tools: 6.8.0


Platform: Windows 10

[REQUIRED] Test case

I had a set of functions built using Node.js version 6. They were successfully deployed and running.

I took the following actions to upgrade to use a newer version of Node:
-- Upgraded my Windows development system to Node.js version 10.15.3
-- Upgraded to latest Firebase tools: npm install -g firebase-tools
-- Upgraded Firebase SDK: npm install firebase-functions@latest firebase-admin@latest --save
-- Edited the project's package.json file to include "engines" : { "node" : "10" }

With those changes, the firebase deploy --only functions command completed without error. However, immediately after deployment, an error log appeared in the Firebase console for each function: Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail.

Changing package.json file to specify version 8 instead of 10 resolved the problem: "engines" : { "node" : "8" } . Functions initialize and execute as expected.

[REQUIRED] Steps to reproduce

Described in test case

[REQUIRED] Expected behavior

Firebase tools should set FIREBASE_CONFIG to the appropriate default for the project.

[REQUIRED] Actual behavior

FIREBASE_CONFIG is not set if Node version of 10 is specified in package.json file.

Most helpful comment

The fix has been rolled out to production. FIREBASE_CONFIG and functions.config() should now work properly in Node 10 runtime. Please let me know if anyone is still seeing issues. Thanks!

All 25 comments

Same problem here. Tried to migrate from 8 to 10 but this happened.

Can't deploy either.

Rolling back to 8 fixed the issue.

Hi @Bob-Snyder thank you for reporting this. This does sound like a bug on our side. I suspect this is because Node 10 runtime no longer sets the GCLOUD_PROJECT environment variable automatically, which is what the functions SDK relies on to set FIREBASE_CONFIG.

The message that you're getting though, is just a warning, so the function should not error when that line is present. Is there another place where the function terminates? Perhaps when the function needs to use admin.database or admin.storage? Would you mind providing that log line?

To help us debug can you provide the code for your function? I assume you have something like this in your function: admin.initializeApp() and I'm curious if initializing with parameters resolves the issue (like providing a databaseUrl if that's what you're using, or storageBucketUrl):

admin.initializeApp({
  databaseURL: 'https://<DATABASE_NAME>.firebaseio.com'
});

I couldn't figure out how to label this issue, so I've labeled it for a human to triage. Hang tight.

@thechenky
When the function executes, the only logs produced are these two:

Function execution started
Function execution took 392 ms, finished with status code: 401

The global initialization for my functions is:

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

const db = admin.firestore();

const settings = {timestampsInSnapshots: true};
db.settings(settings);

Here's one of the functions. As I noted above, when execution fails, none of the expected log messages appear; only the exit status of 401.

exports.httpReportMessage2 = functions.https.onCall((reportMessage, context) => {
  // User must be authenticated.
  if (!context.auth) {
    throw new functions.https.HttpsError('failed-precondition', 'Not authenticated');
  }
  console.log('uid=', context.auth.uid);

  const timeSent = new Date(reportMessage.timeSent);

  console.log('timeSent=', timeSent.toString());

  const elapsed = Date.now() - timeSent;

  if (elapsed > 5000) {
    console.error('Report latency=', elapsed);
  }

  const message = JSON.parse(reportMessage.body);

  const enqueTime = new Date(message.enqueTime);

  const timeInQueue = timeSent - enqueTime;
  if (timeInQueue < 15000) {
    console.log('Time in queue=', timeInQueue);
  } else {
    console.error('Time in queue=', timeInQueue);
  }

  if (validReportMessage(message)) {
    console.log('Valid report message');
    return processReportMessage(message).then(() => {
      return 'Okay';
    }).catch(error => {
      return 'Error';
    });
  } else {
    return 'Invalid report';
  }
});

i have the same issue, and it happens when code access the runtime configuration values set by firebase functions:config:set, in this example it is my cors_origin value:

"use strict";

const cors      = require("cors");
const functions = require("firebase-functions");

exports.cors = cors({
  origin: functions.config().cors_origin   // <------ stacktrace shows this line
});
Detailed stack trace: TypeError: Cannot read property 'cors_origin' of undefined

meaning functions.config() returns undefined

everything works fine on node 8

probably firebase-functions package is outdated and we should wait for it's next release ?

Thanks @rilian for that report as well - these issues are connected since both functions.config() and FIREBASE_CONFIG are populated by reading in the .runtimeconfig.json file, which the SDK can no longer find in the Node 10 runtime.

While I have a pending SDK fix for this, upon further investigating, I think this is a bug in the functions backend, and should be fixed there (see https://github.com/firebase/firebase-functions/pull/434#issuecomment-490168973). We are working with the relevant teams to resolve these issues. Thanks for everyone's patience!

thank you @thechenky

sorry for unrelated question, but which of these functions.config() or FIREBASE_CONFIG is preferred to use across the application, once this will be fixed ?

@rilian they serve different purposes. functions.config() is used if you're storing some custom key value pairs. To set the keys, you would do firebase functions:config:set some-service.key=value. To use that value in your functions code, retrieve it as you did with functions.config().some-service.key (see these docs for more info).

FIREBASE_CONFIG is something that you shouldn't need to worry about - it is automatically populated with your project information and allows you to use the Firebase Admin SDK to talk to Firebase, RTDB, or Storage, for example:

const admin = require('firebase-admin');
// FIREBASE_CONFIG is used here so you can initialize without params here:
admin.initializeApp();

// in your function:
exports.addMessage = functions.https.onRequest((req, res) => {
  // ...
  // FIREBASE_CONFIG helps you be able to use admin like this, by automatically 
  // knowing which Database URL you want to talk to
  admin.database().ref('/messages').push({text: 'hi'});
}

These docs provide more info on what FIREBASE_CONFIG looks like.

Does that help?

Same here, with Node v8 all fine, when I upgrade to Node v10 I get the following error which I provide with the stack trace I found in the Firebase console in case that would help:

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)

I have the same on prod with Node 8 as well

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:144: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)

Only with Firestore triggers though, http function works fine on node v10 and v8.

UPD: converting all functions to async/await syntax, made it run on node 8

Quick update: we have identified and implemented the fix for unpopulated FIREBASE_CONFIG and functions.config() values. This should roll out to production sometime next week.

@MaximBazarov @peterpeterparker your issues are likely different and relating to GCLOUD_PROJECT environment variable missing from the Node 10 runtime (although it should work fine with Node 8). Please open a new issue about this.

@thechenky thx. I opened a new issue, let me know if you need further information

https://github.com/firebase/firebase-functions/issues/437

Just to confirm, I see same error on https callable functions after upgrading to node 10

Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail

Seeing the same error:

Function failed on loading user code. Error message: Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail

Calls to functions.config() return undefined and thus crash the function deployment.

Within the functions emulator I get as well the error Default "firebase-admin" instance created! when calling firebaseAdmin.initializeApp(); without parameters (which worked fine within node v8)

We are experiencing the exact same issue after migrating to firebase-tools@^6.10.0 and the Node 10 runtime:

Warning, FIREBASE_CONFIG environment variable is missing. Initializing firebase-admin will fail
Provided module can't be loaded.
Is there a syntax error in your code?

The detailed stack trace indicates that reading the value from functions.config() returned undefined.

Quick update: we have identified and implemented the fix for unpopulated FIREBASE_CONFIG and functions.config() values. This should roll out to production sometime next week.

Will you close this issue when it rolls out?

wasted an hour trying to figure out why my new function's config was always undefined after i deployed it. Searching google for "firebase function config empty" or "firebase function config undefined" didnt return anything useful.

found this issue only after trying my last resort of reverting the engine from 10 to 8... :(

The fix has been rolled out to production. FIREBASE_CONFIG and functions.config() should now work properly in Node 10 runtime. Please let me know if anyone is still seeing issues. Thanks!

C:\Users...> firebase deploy --only functions

=== Deploying to 'xxxxxxx'...

i  deploying functions
i  functions: ensuring necessary APIs are enabled...
+  functions: all necessary APIs are enabled
i  functions: preparing functions directory for uploading...

Error: package.json in functions directory has an engines field which is unsupported. The only valid choices are: {"node": "8"} and {"node": "6"}.

C:\Users...> firebase -V
6.5.0
C:\Users...> node --version
v10.15.3

Update firebase-tools

@thechenky still not working.

@skhaz what exactly is not working? I'm going to need more information than that...

@thechenky Sorry for the lack of information.

I tried a deploy using NodeJs 10 and got:

Error: process.env.GCLOUD_PROJECT is not set. at DocumentBuilder...

My package.json

{
  "name": "functions",
  "version": "1.0.0",
  "main": "dist/index.js",
  "private": true,
  "devDependencies": {
    "firebase-tools": "^6.10.0",
    "parcel-bundler": "^1.12.3",
    "rimraf": "^2.6.3"
  },
  "dependencies": {
    "firebase-admin": "^8.0.0",
    "firebase-functions": "^2.3.1"
  },
  "engines": {
    "node": "10"
  },
  "scripts": {
    "build": "rimraf dist && parcel build src/index.js --target node --detailed-report",
    "deploy": "yarn build && firebase deploy --force --only functions",
  }
}

This is a separate issue: https://github.com/firebase/firebase-functions/issues/437. Please follow that bug.

Ok. Thanks.

Was this page helpful?
0 / 5 - 0 ratings