Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting NODE_ENV for firebase function

Tags:

I am moving some of my firebase-queue workers to Firebase Functions. I have used process.env.NODE_ENV to set some of the configuration for the workers depending on the environment in which I am running them. Is there a way to set the NODE_ENV for the functions while deploying them. I understand that the recommended way to provide such config options is via firebase.config.set which I have verified works as expected but just wanted to check if there is a way to set the NODE_ENV also. When I try to print out the NODE_ENV inside of a function, it is always set to production.

like image 646
Varun Gupta Avatar asked Mar 31 '17 11:03

Varun Gupta


People also ask

How do I see GCP environment variables?

Open the Runtime, build and connections settings section. Select the Runtime tab. In the Runtime environment variables section, click Add variable and add the name and value. For instructions on how to add environment variables to an existing function, see Updating runtime environment variables.


2 Answers

Following Google's Best practices and reserved environment variables in their documentation

Environment variables that are provided by the environment might change in future runtime versions. As a best practice, we recommend that you do not depend on or modify any environment variables that you have not set explicitly.

Basically don't use NODE_ENV. Use your own environment variables and set them accordingly.

NOTE: This documentations is from Google Cloud Functions. Firebase functions are like a wrapper around google cloud functions. Check this question

like image 54
ajorquera Avatar answered Sep 19 '22 08:09

ajorquera


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 functions and you need to use the gcloud command-line tool.

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

Other options

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

Conditions based on project ID

You can access the project id if you are having test, staging and production projects and want to have different behavior or logging depending on the environment.

process.env.GCLOUD_PROJECT is set to your GCP project ID so you can build logic based on that.

 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 there's the cloud functions environment variables also. You can effectively create build pipelines that are configuring your environment configuration upon build/deploy and later access them in your cloud function.

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

Accessing the configuration is effectively the same as your process.env but can not be accessed outside of the scope of a cloud function (i.e. you can't use it in a global variable declaration).

if (functions.config().runtime.env === 'production') {   // Only in production } else {   // Do something for the test environments } 
like image 43
Dennis Alund Avatar answered Sep 17 '22 08:09

Dennis Alund