How to detect if environment is development or production with Firebase Cloud Functions?

Viewed 8218

How can I detect if my server environment is development or production with Firebase Cloud Functions?

I need something like this:

if(process.env.NODE_ENV === 'development'){

   //DO STUFF SPECIFIC TO DEV ENVIRONMENT

}
else if(process.env.NODE_ENV === 'production'){

   //DO STUFF SPECIFIC TO PRODUCTION ENVIRONMENT

}
3 Answers

process.env.FUNCTIONS_EMULATOR

At process.env, on firebase functions projects, there is a boolean variable called FUNCTIONS_EMULATOR, which indicates if the process is running on an emulator or on server.

this is enough to determine if environment is dev or production.

process.env.FUNCTIONS_EMULATOR === true

Obs: In some environments the variable might be a String 'true' instead of Boolean true

you can rely on process.env

as of July 28, 2020 and package.json

"dependencies": {
    "firebase-admin": "^8.10.0",
    "firebase-functions": "^3.6.1"
},

if you start your app with firebase

firebase emulators:start

thenprocess.env will have properties like

"FUNCTIONS_EMULATOR": "true",
"FIRESTORE_EMULATOR_HOST": "0.0.0.0:5002",
"PUBSUB_EMULATOR_HOST": "localhost:8085"
"FIREBASE_AUTH_EMULATOR_HOST": "0.0.0.0:9099"

if you start your app with firebase

firebase emulators:start --only functions

thenprocess.env will have properties like

"FUNCTIONS_EMULATOR": "true",

USECASE

based on process.env you can write firebase.function to prepopulate your firestore emulator (not production firestore)!

code sample

export const prepopulateFirestoreEmulator = functions.https.onRequest(
  (request, response) => {
    if (process.env.FUNCTIONS_EMULATOR && process.env.FIRESTORE_EMULATOR_HOST) {
      // TODO: prepopulate firestore emulator from 'yourproject/src/sample_data.json
      response.send('Prepopulated firestore with sample_data.json!');
    } else {
      response.send(
        "Do not populate production firestore with sample_data.json"
      );
    }
  }
);

All Google Cloud projects are just projects, except for how you designate their purpose. Since there is no way for Cloud Functions to know the difference between dev and prod, you need to examine the name of the project, since that's the only thing that changes in the environment. Use process.env.GCLOUD_PROJECT from the automatically populated env vars.

It seems that other answers here are making the assumption that this question is asking about local development, which it is not. It's easy to detect if code is running in the emulator, but that does not help you determine if the underlying project is meant for development or production (or some other purpose). It's still very possible for some code running in the emulator to have an unwanted effect only the underlying project, which would be bad for a project designated as production. It's strongly advised to run and isolate different projects for different deployments so changes in one do not affect the other.

Related