Firebase-functions: Migration Problem with the new process.env.FIREBASE_CONFIG

Created on 5 Apr 2018  路  6Comments  路  Source: firebase/firebase-functions

Version info

"firebase-admin": "5.11.0",
"firebase-functions": "1.0.0",
"firebase-tools": "3.18.0",

Test case

I've migrated from functions beta to v1, but I'm having issues initializing firebase-admin since migrating.
My setup was a main index.ts which called admin.initializeApp(functions.config().firebase); at the top of my index.ts and then exports each firebase function below.
Since the change from admin.initializeApp(functions.config().firebase); to the new admin.initializeApp(), I've been receiving the error:

Error: Can't determine Firebase Database URL.
    at FirebaseDatabaseError.Error (native)
    at FirebaseDatabaseError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:39:28)
    at new FirebaseDatabaseError (/user_code/node_modules/firebase-admin/lib/utils/error.js:190:23)
    at DatabaseService.ensureUrl (/user_code/node_modules/firebase-admin/lib/database/database.js:75:15)
    at DatabaseService.getDatabase (/user_code/node_modules/firebase-admin/lib/database/database.js:52:26)
    at FirebaseApp.database (/user_code/node_modules/firebase-admin/lib/firebase-app.js:235:24)

I've determined that even attempting to console.log(process.env.FIREBASE_CONFIG) from the top of my index.ts prints undefined. However, printing the above from within each function export will print the config and allow me to initialize with admin.initializeApp().

It looks like I can workaround the change by adding admin.initializeApp() within each of my function exports, but I'd like to understand what's really going on. It's as if process.env.FIREBASE_CONFIG isn't set until each function is exported, whereas the beta version firebase.config().firebase was available instantly.
Is it recommended to initializeApp within each function?

Most helpful comment

Thanks for filing! This issue is fixed in firebase-tools 3.18.1, which was just released.

All 6 comments

I have this similar issue. I have an index.ts which calls admin.initializeApp() and requires files that export functions.

Can you provide a specific example for the workaround? I tried adding admin.initializeApp() to one of my exported functions (files), but got this error.

Error: The default Firebase app already exists. This means you called initializeApp() more than once without providing an app name as the second argument. In most cases you only need to call initializeApp() once. But if you do want to initialize multiple apps, pass a second argument to initializeApp() to give each app a unique name.

@zgosalvez As a temporary workaround, I've wrapped admin.initializeApp() with a try catch.
_I've got a library that I import in every firebase function, and I've added the initialize in there rather than repeatedly in every function._

Thanks for filing! This issue is fixed in firebase-tools 3.18.1, which was just released.

As always, thank you @laurenzlong. I tried deploying using the latest firebase-tools version but failed. No code changes on my part. I filed a new issue firebase/firebase-tools#722 which is still related to admin.initializeApp().

The fix works for me @zgosalvez with a single initializeApp() _without_ the try catch, all back to normal.

A fix + lib release in less than 22hrs since opening,
@laurenzlong can I send 馃崺's to the Firebase HQ? 鉂わ笍

@laurenzlong This is still an issue for me. Specifically, the error occurs at least in one of my HTTP trigger functions using express. Should I create a separate issue?

Was this page helpful?
0 / 5 - 0 ratings