Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you setup local environment variables for Cloud Functions for Firebase

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?

like image 295
ChrisMacSEA Avatar asked Jun 26 '17 18:06

ChrisMacSEA


People also ask

Does Firebase run locally?

The Firebase Local Emulator Suite is a set of advanced tools for developers looking to build and test apps locally using Cloud Firestore, Realtime Database, Cloud Storage for Firebase, Authentication, Firebase Hosting, Cloud Functions (beta), Pub/Sub (beta), and Firebase Extensions (beta).


2 Answers

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

like image 183
laurenzlong Avatar answered Oct 01 '22 13:10

laurenzlong


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]; } 
like image 30
Muthukumar Avatar answered Oct 01 '22 14:10

Muthukumar