Firebase-functions: process.env.FIREBASE_CONFIG is not populated

Created on 16 Dec 2019  ยท  12Comments  ยท  Source: firebase/firebase-functions

[REQUIRED] Step 2: Describe your environment

  • Operating System version: Linux 7u 5.3.0-24-generic firebase/firebase-admin-node#26-Ubuntu SMP Thu Nov 14 01:33:18 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux
  • Firebase SDK version: 7.10.0
  • Firebase Product: firebase-functions/firebase-admin
  • Node.js version: nodejs10 runtime
  • NPM version: 6.13.1

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

Relevant Code:

I was trying to adopt https://firebase.google.com/docs/functions/config-env#automatically_populated_environment_variables.
Deploying firebase function (http) with:

if (process.env.FIREBASE_CONFIG === undefined) {
    throw new Error('Missing FIREBASE_CONFIG');
}
...
admin.initializeApp(opts);

makes deployment to fail.

From terminal:

$ firebase deploy --only functions:admin.forceRcUpdate
โœ” scheduler: all necessary APIs are enabled
โš  functions[admin-forceRcUpdate(us-central1)]: Deployment error.
Error setting up the execution environment for your function. Please try deploying again after a few minutes.

In logs:

E 2019-12-16T07:16:02.599384474Z admin-forceRcUpdate Error: function terminated. Recommended action: inspect logs for termination reason. Function cannot be initialized.
admin-forceRcUpdate
A 2019-12-16T07:15:58.840Z admin-forceRcUpdate Could not load the function, shutting down. admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Object. (/srv/node_modules/@google-cloud/functions-framework/build/src/index.js:67:33) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Object.getUserFunction (/srv/node_modules/@google-cloud/functions-framework/build/src/invoker.js:57:32) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at require (internal/modules/cjs/helpers.js:22:18) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Module.require (internal/modules/cjs/loader.js:637:17) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Function.Module._load (internal/modules/cjs/loader.js:531:3) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at tryModuleLoad (internal/modules/cjs/loader.js:539:12) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Module.load (internal/modules/cjs/loader.js:600:32) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Module._compile (internal/modules/cjs/loader.js:701:30) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate at Object. (/srv/functions/src/index.ts:12:11) admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate Detailed stack trace: Error: Missing FIREBASE_CONFIG admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate Is there a syntax error in your code? admin-forceRcUpdate
A 2019-12-16T07:15:58.837Z admin-forceRcUpdate Provided module can't be loaded. admin-forceRcUpdate

Removing access to process.env.FIREBASE_CONFIG fixes the issue.
Local emulator works well with it

Attention

Most helpful comment

@stari4ek To import a module just for its side effects, use this syntax:
import 'firebase-functions';
https://www.typescriptlang.org/docs/handbook/modules.html#import-a-module-for-side-effects-only

All 12 comments

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

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

Hi, thanks for submitting your issue. What version of Firebase CLI firebase-tools are you using?

$ npm list | grep firebase
โ”œโ”€โ”ฌ [email protected]
โ”‚ โ”œโ”€โ”ฌ @firebase/[email protected]
โ”‚ โ”‚ โ”œโ”€โ”ฌ @firebase/[email protected]
โ”‚ โ”‚ โ”‚ โ””โ”€โ”€ @firebase/[email protected]
โ”‚ โ”‚ โ”œโ”€โ”€ @firebase/[email protected]
โ”‚ โ”‚ โ”œโ”€โ”ฌ @firebase/[email protected]
โ”œโ”€โ”ฌ [email protected]
โ”œโ”€โ”ฌ [email protected]

$ npm -g list | grep firebase
โ”œโ”€โ”ฌ [email protected]

Same behavior with [email protected]

@stari4ek When you downgrade to Node8 do you face this error? We had related issues with Node10 several months back and that bug seems to have been fixed. Just want to confirm.

With most recent firebase-tools (7.11.0).
nodejs8 (creating new function or updating existing nodejs10):

i  functions: updating Node.js 8 function admin-someFunc(us-central1)...
โš   functions[admin-someFunc(us-central1)]: Deployment error.
Function failed on loading user code. Error message: Code in file lib/src/index.js can't be loaded.
Is there a syntax error in your code?
Detailed stack trace: Error: Missing FIREBASE_CONFIG
    at useServiceAccountCredentials (/srv/src/index.ts:16:15)
    at Object.<anonymous> (/srv/src/index.ts:23:3)
    at Module._compile (module.js:653:30)
    at Object.Module._extensions..js (module.js:664:10)
    at Module.load (module.js:566:32)
    at tryModuleLoad (module.js:506:12)
    at Function.Module._load (module.js:498:3)
    at Module.require (module.js:597:17)
    at require (internal/module.js:11:18)
    at getUserFunction (/worker/worker.js:439:24)

code:

import 'source-map-support/register';
import * as admin from 'firebase-admin';

const useServiceAccountCredentials: boolean = (() => {
    if (process.env.FIREBASE_CONFIG === undefined) {
        throw new Error('Missing FIREBASE_CONFIG');
    }
   ...
})();

BTW, with nodejs10 deployment takes much longer till it fails and there is no error message from cli:

i  functions: updating Node.js 10 (Beta) function admin-someFunc(us-central1)...
โš   functions[admin-someFunc(us-central1)]: Deployment error.
Error setting up the execution environment for your function. Please try deploying again after a few minutes.

starts well in emulator:

$ firebase serve --only functions:admin.someFunc
โš   Your requested "node" version "10" doesn't match your global version "12"
โœ”  functions: Emulator started at http://localhost:5000
i  functions: Watching "/home/user/dev/prj/develop/fb/functions" for Cloud Functions...
โš   functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to .... Non-emulated services will access production using these credentials. Be careful!
...
โœ”  functions[admin-someFunc]: http function initialized (http://localhost:5000/prj-dev/us-central1/admin-someFunc).

@stari4ek I can't repro your error with the code snippet you provided. Can you run using the debug flag to see the full trace? firebase deploy --only functions:admin.forceRcUpdate --debug

I can't tell from your code snippet - but are you importing firebase-functions before you try to access FIREBASE_CONFIG?

I do not import firebase-functions in index.ts since I do not use anything from it in index.ts.
All functions are implemented in their own modules and they are exported using technique:

import { someFunc } from './someModule'
if (!process.env.FUNCTION_NAME || process.env.FUNCTION_NAME === 'someFunc') {
    exports.someFunc = someFunc;
}

Adding

import * as functions from 'firebase-functions';

does fix the issue.

But firebase-functions does export a lot of stuff I do not need, and I'd prefer to import/call what is really used (plus any IDE will strip those import automatically as non-referenced)
Is there public module/function to setup env explicitly?

@stari4ek To import a module just for its side effects, use this syntax:
import 'firebase-functions';
https://www.typescriptlang.org/docs/handbook/modules.html#import-a-module-for-side-effects-only

did check with import 'firebase-functions';
works well.

I'm not sure if this is good practice in ts/js, but, honestly speaking, for me it looks more like implementation-specific stuff leaking outside the module during it's import.

This issue could be closed.
Thank you

i have same problem, i use node 10, firebase-function 3.7.0 firebase-admin 8.10.0,
function terminated. Recommended action: inspect logs for termination reason. Function cannot be initialized

Was this page helpful?
0 / 5 - 0 ratings