How do you setup local environment variables for Cloud Functions for Firebase
FirebaseGoogle Cloud-FunctionsFirebase Problem Overview
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?
Firebase Solutions
Solution 1 - Firebase
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
Solution 2 - Firebase
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];
}
Solution 3 - Firebase
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"
}
}
Solution 4 - Firebase
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.
Solution 5 - Firebase
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\""
}
}
Solution 6 - Firebase
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"