87

I'm using http cloud functions to listen for a request and then return a simple message.

I'm developing cloud functions locally using:

firebase serve --only functions

I've setup some custom environment variables using

firebase functions:config:set

Accessing the custom config variables using the below code works fine when the project is deployed

 functions.config()

but it does not work when developing locally. When the function is triggered by hitting: http://localhost:5002/my-project-name/us-central1/functionName I can't access the custom config variables. when using functions.config() locally, I can see the default config, just not my custom config variables

Is there an alternate solution or best practice for environment variables when working locally?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
ChrisMacSEA
  • 891
  • 1
  • 6
  • 6
  • Could you be more specific about what you're doing when you say that you're developing locally? – Doug Stevenson Jun 27 '17 at 00:46
  • @DougStevenson I updated the question. Hopefully that provides a bit more clarity about what I'm doing – ChrisMacSEA Jun 28 '17 at 15:26
  • I'm seeing the same issue. I'll file a bug internally on this (I'm with the Firebase team). Feel free to also leave a bug report at https://firebase.google.com/support/contact/bugs-features/ – Doug Stevenson Jun 28 '17 at 23:45

6 Answers6

158

As of now, you have to manually create a .runtimeconfig.json file inside your functions directory by running this command. Then run the serve command.

firebase functions:config:get > .runtimeconfig.json

If you are using Windows Powershell, replace the above with:

firebase functions:config:get | ac .runtimeconfig.json

You can learn more in https://firebase.google.com/docs/functions/local-emulator

Brett DeWoody
  • 59,771
  • 29
  • 135
  • 184
laurenzlong
  • 2,169
  • 1
  • 13
  • 13
  • Could you detail the local config set in order to test function that send FCM notifications ? – ThierryC Nov 14 '17 at 09:10
  • You shouldn't have to do anything special. The emulator automatically populates functions.config().firebase with the right project credentials. So the following lines will work locally: const admin = require('firebase-admin'); admin.initializeApp(functions.config().firebase); – laurenzlong Nov 14 '17 at 19:08
  • And here's the code sample for using FCM within a function: https://github.com/firebase/functions-samples/blob/master/fcm-notifications/functions/index.js – laurenzlong Nov 14 '17 at 19:08
  • 1
    My trigger launch a FCM notificatin on User Creation. it works on Firebase server but tested locally I have the error : "info: Function crashed { Error: An error occurred when trying to authenticate to the FCM servers. Make sure the credential used to authenti cate this SDK has the proper permissions. See https://firebase.google.com/docs/admin/setup for setup instructions. R aw server response: " Unauthorized

    Unauthorized

    Error 401

    ". Status code: 401."
    – ThierryC Nov 16 '17 at 10:45
  • Oh interesting, can you file a bug to https://github.com/firebase/firebase-tools/issues, and follow the template to add repro instructions? Thanks! – laurenzlong Nov 16 '17 at 18:57
  • Works march 4, 2018 ty! – Gavin Thomas Mar 04 '18 at 19:45
  • 5
    Slight note here that the json file referenced above has to be in your `functions` directory like the post mentioned, so just running the above snipped w/o being in the functions directory first (say if you're in the root of the firebase proj instead) won't work. In that case, you'd have to run something like `firebase functions:config:get > functions/.runtimeconfig.json` instead. – Keegan Brown Jun 11 '18 at 22:28
  • 7
    Not working for me. I have the ".runtimeconfig.json" in the "functions" dir generated with "functions:config:get > .runtimeconfig.json". But when I am in localhost and execute "functions.config()" in my project it returns an empty object. – Cabuxa.Mapache Aug 03 '18 at 09:29
  • In Windows you must exec: firebase functions:config:get | ac .runtimeconfig.json – Cabuxa.Mapache Aug 03 '18 at 10:06
  • This is also noted in the documentation https://firebase.google.com/docs/hosting/functions – ralphinator80 May 15 '19 at 00:05
  • 2
    @Cabuxa.Mapache did you figure out why it was returning an empty object? I'm having the same issue because whenever I run `firebase functions:config:get > .runtimeconfig.json` on Mac it actually replaces all of the data in that file with an empty JSON object... I tried it with different filenames like `.devConfig.json` and `.serveConfig.json`, but whatever filename I provide in the command gets overwritten with an empty JSON object.. – Kat Sep 14 '19 at 17:40
  • 1
    @Cabuxa.Mapache I also get this result (empty dictionary). Any thoughts on how to solve? – Charklewis Sep 22 '19 at 18:43
16

For those who want to use the environment variables (process.env), I follow this workaround.

Set the config values before deploying

firebase functions:config:set envs.db_host=$DB_HOST_PROD envs.db_user=$DB_USER_PROD envs.db_password=$DB_PASSWORD_PROD envs.db_name=$DB_NAME_PROD envs.db_use_ssl=false

Read the config and update the env variables first thing under your functions code.

const functions = require('firebase-functions');
const config = functions.config();

// Porting envs from firebase config
for (const key in config.envs) {
    process.env[key.toUpperCase()] = config.envs[key];
}
Abraham
  • 8,525
  • 5
  • 47
  • 53
Muthukumar
  • 8,679
  • 17
  • 61
  • 86
5

You can keep a file called .env.json and load it when you trigger deploy command

{
  "name": "project",
  "version": "0.0.0",
  "scripts": {
    "deploy": "npm run env && firebase deploy --only functions",
    "env": "test -f env.json && firebase functions:config:unset env && firebase functions:config:set env=\"$(cat env.json)\" || echo \"Please add the file env.json before deploy.\""
  },
  "dependencies": {
    "firebase-functions": "^3.1.0"
  },
  "devDependencies": {
    "firebase-functions-test": "^0.1.6"
  }
}
benomatis
  • 5,536
  • 7
  • 36
  • 59
Codex
  • 1,153
  • 1
  • 20
  • 31
2

I've narrowed down the issue to Windows Powershell.

Running firebase functions:config:get > .runtimeconfig.json in powershell generates a broken json I don't know why, which when parsed gives Unexpected token � in JSON at position 0.

I've managed to sort it out by running .runtimeconfig.json generation command in Windows command prompt.

d-_-b
  • 21,536
  • 40
  • 150
  • 256
saran0798
  • 21
  • 1
2

If you are using Nrwl NX, you will have to generate your .runtimeconfig.json inside of the dist/apps/functions directory.

Example package.json:

{
  "scripts": {
    "firebase:emulators:start": "firebase functions:config:get > dist/apps/functions/.runtimeconfig.json && env-cmd firebase emulators:start --export-on-exit=\".firebase-emulator\" --import=\".firebase-emulator\""
  }
}
Exac
  • 53
  • 1
  • 2
  • 7
0

I am not sure if the top-rated answer works or not but for firebase function on mac (to-serve locally), I do something like this

npm run admin-keys && export dev=true && firebase emulators:start

Where admin keys is

"admin-keys": "export GOOGLE_APPLICATION_CREDENTIALS='./.keys/admin.keys.json'"

This will load configuration from .runtimeconfig.json

For production, you would manually have to set it by doing something like this

firebase functions:config:set facebookCred.secret="something"
Alwaysblue
  • 9,948
  • 38
  • 121
  • 210