How do you setup local environment variables for Cloud Functions for Firebase
Asked Answered
G

6

88

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?

Galwegian answered 26/6, 2017 at 18:40 Comment(3)
Could you be more specific about what you're doing when you say that you're developing locally?Yard
@DougStevenson I updated the question. Hopefully that provides a bit more clarity about what I'm doingGalwegian
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 firebase.google.com/support/contact/bugs-featuresYard
D
159

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

Demon answered 12/7, 2017 at 17:35 Comment(12)
Could you detail the local config set in order to test function that send FCM notifications ?Perspiratory
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);Demon
And here's the code sample for using FCM within a function: github.com/firebase/functions-samples/blob/master/…Demon
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 firebase.google.com/docs/admin/setup for setup instructions. R aw server response: "<HTML> <HEAD> <TITLE>Unauthorized</TITLE> </HEAD> <BODY BGCOLOR="#FFFFFF" TEXT="#000000"> <H1>Unauthorized</H1> <H2>Error 401</H2> </BODY> </HTML> ". Status code: 401."Perspiratory
Oh interesting, can you file a bug to github.com/firebase/firebase-tools/issues, and follow the template to add repro instructions? Thanks!Demon
Works march 4, 2018 ty!Trinitytrinket
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.Condottiere
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.Clockmaker
In Windows you must exec: firebase functions:config:get | ac .runtimeconfig.jsonClockmaker
This is also noted in the documentation firebase.google.com/docs/hosting/functionsTsar
@Clockmaker 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..Granado
@Clockmaker I also get this result (empty dictionary). Any thoughts on how to solve?Testaceous
V
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];
}
Vibraphone answered 3/5, 2019 at 17:53 Comment(0)
I
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"
  }
}
Informal answered 9/5, 2020 at 19:27 Comment(0)
P
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.

Pera answered 31/8, 2020 at 0:50 Comment(0)
N
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\""
  }
}
Nutty answered 27/1, 2022 at 23:14 Comment(0)
D
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"
Drunk answered 26/9, 2019 at 7:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.