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
.
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.
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
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
If you are developing specifically for Firebase and need a similar solution, then there are options.
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 }
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 }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With