Setting NODE_ENV for the firebase function

I am moving some of my working firebase-queue into Firebase functions. I used process.env.NODE_ENV to set up some configuration for workers depending on the environment in which I run them. Is there a way to set NODE_ENV for functions when they are deployed. I understand that the recommended way to provide such configuration parameters through firebase.config.set , which I checked, works as expected, but just wanted to check if there is a way to set NODE_ENV . When I try to print NODE_ENV inside a function, it is always set to production .

+14
source share
3 answers

Following the Google Best practices and reserved environment variables in their documentation

The environment variables provided by the environment may change in future versions of the runtime. It is recommended that you do not depend on or modify environment variables that you did not explicitly specify.

Basically do not use NODE_ENV . Use your own environment variables and set them accordingly.

NOTE. This documentation is taken from Google's cloud features. Firebase features are similar to the Google Cloud Shell features. Check this question

+1
source

There is currently no way to set custom environment variables such as process.env.NODE_ENV . What you want to do can only be done for Google Cloud features, and you need to use gcloud command line.

https://cloud.google.com/functions/docs/env-var#accessing_environment_variables_at_runtime

Other options

If you are developing specifically for Firebase and you need a similar solution, then there are options.

Project ID based conditions

You can access the project ID if you have test, intermediate, and production projects, and you want to behave differently or keep a log depending on the environment.

process.env.GCLOUD_PROJECT the identifier of your GCP project is set, so you can build logic based on it.

 if (process.env.GCLOUD_PROJECT === 'my-production-project') { // Only in production } else { // Do something for the test environments } 

Cloud function environment variables

As you already mentioned, cloud functions are also environment variables. You can efficiently create assembly pipelines that configure your environment during assembly / deployment, and then access them in your cloud function.

 - firebase functions:config:set runtime.env="production" --token $FIREBASE_DEPLOY_KEY 

Access to the configuration is actually the same as for your process.env but it cannot be accessed outside the cloud function (i.e. you cannot use it in the declaration of a global variable).

 if (functions.config().runtime.env === 'production') { // Only in production } else { // Do something for the test environments } 
0
source

As I answer this question, the Firebase SDK for cloud features offers an in-box environment configuration out of the box .

Set the environment configuration for your project

 $ firebase functions:config:set [values...] 

example

 $ firebase functions:config:set someservice.key="THE API KEY" someservice.id="THE CLIENT ID" 

Get environment configuration for your project

 $ firebase functions:config:get [path] 

example

 const functions = require('firebase-functions') console.log(functions.config().someservice.id) 

You must redeploy functions to make the new configuration available.

-1
source

Source: https://habr.com/ru/post/1266165/


All Articles