3

I was using the environment variables from Cloud Functions package the old way by using the functions.config() command, but since I updated to v1.0.2 I cannot use the env variables even through JSON.parse(process.env.FIREBASE_CONFIG) like the documentation tells to do, and I couldn't find how to set or get other information. This command only gives me information about the project but not the information I have set using the old approach. How can I get/set this information using the new approach?
Thanks in advance.

william
  • 585
  • 3
  • 14
  • I don't understand your question. You still access environment variables set by the Firebase CLI using `functions.config()`. That hasn't changed at all. There is just no longer a `functions.config().firebase`. – Doug Stevenson May 02 '18 at 17:09
  • Oh, I misunderstood the documentation. Do you want to write the same thing as an answer so I can accept it as the correct answer? Thank you very much for the heads up again, Doug. – william May 02 '18 at 18:42

3 Answers3

3

The documentation for the environment changes in 1.0 can be a little confusing. functions.config() isn't going away - you still use that to access environment variables that you set using the Firebase CLI with firebase functions:config:set. The things that changed with respect to configuration are the following:

  • functions.config().firebase is no longer used for project configuration. That information is now stored in the process environment as process.env.FIREBASE_CONFIG.
  • You can now initialize the Admin SDK with no arguments as admin.initializeApp(). The configuration will be picked up from the Cloud Functions runtime automatically.
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0

So check out this answer here https://stackoverflow.com/a/45064266/4871483. What I understand is that the new cloud functions will work from a local .runtimeconfig.json. What I would recommend is temporarily downgrade and write your old config to that file. Then you can use these docs https://firebase.google.com/docs/functions/local-emulator for the code on how to access it once you upgrade again.

rassa45
  • 3,482
  • 1
  • 29
  • 43
  • Thank you for the response, but this is not what I meant. I misunderstood the documentation by thinking that the `functions.config()` method was not working anymore. I should not have changed the approach for getting my custom information. – william May 02 '18 at 18:43
  • Ah I see. I'll leave the answer up there if anyone else needs to do the migration or local environment variables – rassa45 May 02 '18 at 18:45
0

What I do is basically have

env.prod.ts
env.dev.ts
env.ts

and in env.ts I do

export const env = require(`./env.${functions.config().env.name}').default;

and in package.json i set

"dev-prod": firebase use prod && functions:config:set env.name=prod

etc so I can have both fb keys and my own prod keys and the whole app is config-agnostic

when i need env var I just use

import environment from '../env.ts'

works with json etc(dotenv doesnt)

repo
  • 748
  • 1
  • 8
  • 19