Setting NODE_ENV for firebase function

firebase functions environment variables
firebase functions:config:clone
admin initializeapp functions:config firebase
firebase functions set environment variable
non existent functions:config value requested
firebase functions:config remove
firebase functions source
firebase function groups

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.

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

Environment configuration, You can also set environment variables during function creation in the runtime (for example, NODE_ENV , VIRTUAL_ENV , or GOPATH ). Often you'll need additional configuration for your functions, such as third-party API keys or tuneable settings. The Firebase SDK for Cloud Functions offers built-in environment configuration to make it easy to store and retrieve this type of data for your project.

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
}

Using Environment Variables, Currently, the firebase QA project's app function is reading from the production database. Article for setting environment variables in firebase:� Firebase Cloud Functions, (or running code on Firebase Servers!) Tools used: Backend: Firebase Cloud Functions -- Version: 3.0.0 Taken straight from the official website: Firebase Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code.

At the time I'm answering this question the Firebase SDK for Cloud Functions offers built-in environment configuration out of the box.

Set 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.

Firebase QA project needs to set process.env.NODE_ENV to QA , [REQUIRED] Version info node: v10.15.0 firebase-functions: ^3.0.0 with NO config values, just empty .json) - so i have to manually set the file in this NODE_ENV || firebaseEnv || 'development', root: path.join(__dirname, '. Some additional environment variables are automatically set depending on the runtime your function uses. These are based on the runtime's operating system (for example, DEBIAN_FRONTEND, SHLVL, or PATH) and the language runtime (for example, NODE_ENV, VIRTUAL_ENV, or GOPATH).

[functions.config()] is EMPTY when running [firebase serve] or , Plugin that allows you to render AdminBro by Firebase Cloud Functions and run this script before the deployment with NODE_ENV set to production. Modules might have the different behaviors (like logging) depending on the value of the NODE_ENV variable. What is process.env in Node.js The Node injects the process.env global variable at runtime in our app to use, and it represents the state of the system environment of our app when it starts.

admin-bro-firebase-functions, functions:set db4.host="myHostIP", db4.user="myUsername" Then I publish the functions: firebase deploy --� Took me a while to figure out it was because I'd tweaked the render function to enable dev in the config object when it's set in the node ENV (config.dev = process.env.NODE_ENV !== 'production'). If you want to be able to run the code in dev mode for debugging purposes just add srcDir: '../src' to the config object.

Firebase Environment Variables have me losing my mind, Cloud Functions is a hosted, private, and scalable Node.js environment where you can run JavaScript code. The Firebase SDK for Cloud� 7 Setting NODE_ENV for firebase function Mar 16 '19. 6 Firebase crashlytics doesnt show crashes Jan 2 '19.

Comments