1

I'm developing an application with my team with Firebase, and I want to deploy my app both in development and production environments.

To achieve this, I have done several things. (Details are below)
But NODE_ENV keeps set as 'development' when I deploy it to firebase functions app.
How can I deploy it as 'development' to firebase?

Those are what I have done to achieve my goal.

  • Make another firebase project and function for 'production' (Use current firebase project as 'development')
    firebase console
  • Edit .firebaserc file and set aliases for my projects (to use firebase --use command)
    .firebaserc
  • Separate RDS Instance and Slack Monitoring app
  • Separate .env files with .env.dev and .env.prod and use secrets based on NODE_ENV
    set .env files
  • Add 'dev' script to package.json to deploy as 'NODE_ENV = development'
    package.json scripts

This is the code I wrote to find out is which environment my server is running
node_env console.log
And this is from my github actions log and firebase console log
github actions and firebase console log

When I run my app in local with 'serve' command, console.log prints 'development' as I expected.
I guess 'firebase deploy' command automatically changes my NODE_ENV to production when it is deployed.
Or did I make some mistakes to do this?

1 Answer 1

4

The recommended way to have a development and production environment is to have two separate Firebase projects, which you are already making use of. For the sake of an example, let's assume you have hyositive-app as your production project and hyositive-dev as your development project.

Defining Deployed Environment Variables

Use of environment variables with Cloud Functions for Firebase are based on .env files compatible with the dotenv package. By default, the following files are supported (ignoring others!):

Name                              Purpose
.env Environment variables common to all environments
.env.<projectID> Environment variables used when deployed to the named project (e.g. .env.hyositive-app)
.env.<alias> Environment variables used when deployed to the aliased project (e.g. .env.dev).
Note: default is the only alias configured out of the box. dev/prod/etc. are not defined.
.env.local Environment variables used when using emulating functions on your system using the Cloud Functions emulator.

To use .env.dev and .env.prod, you will need to define them as project aliases in your .firebaserc file (or you can continue using development and production aliases and just update the filenames to match):

{
  "projects": {
    "default": "hyositive-dev",
    "dev": "hyositive-dev",
    "prod": "hyositive-app"
  }
}

This then allows you to use .env.dev and .env.prod instead of .env.hyositive-dev and .env.hyositive-app respectively.

Using Environment Variables

The Cloud Functions runtime (the process that runs your deployed code) defines a number of built-in environment variables that have various purposes (such as allowing you to use initializeApp() with no arguments).

In addition to these variables, a handful of language-specific variables are also defined by the runtime to aid in painless deployment of code. However, the Cloud Functions documentation states to not rely on their values unless you set the values yourself.

The Node.js Cloud Functions runtime is built using the Cloud Functions Framework (however, it is not guaranteed to perfectly match this open source version). Because this runtime executes using Node.js and makes use of other packages such as express, it sets NODE_ENV to production, to minimise unexpected behaviour that depends on its value. But as mentioned above, this behaviour should not be relied on even though it is unlikely to change.

To override NODE_ENV to development, you would add it into .env.dev, .env.hyositive-dev and/or .env.local (as appropriate). Similarly, you should also define NODE_ENV as production in .env.prod or .env.hyositive-app (as appropriate).

Rather than rely on NODE_ENV, I would recommend defining behaviour around another variable that you have complete control over (such as HYOSITIVE_ENV) or compare against the executing project ID to determine whether it is running in the production project or not.

const PROD_PROJECT_ID = "hyositive-app",
   // DEV_PROJECT_ID = "hyositive-dev",
      PROJECT_ID = JSON.parse(process.env.FIREBASE_CONFIG).projectId,
      isProduction = PROJECT_ID === PROD_PROJECT_ID;

Note: This other thread may have some useful background information.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow Thank you so much! I learned a lot from you!! Wish you all the best :)
Hello, regrettably, the process.env variables are only defined during runtime. The issue arises because our functions rely on numerous node_modules, each of which configures itself by accessing certain environmental variables. As these modules are imported, they execute prior to runtime, resulting in the expected environmental variables being undefined. How can I address this challenge?
By pointing both dev and prod to the same project, you can have multiple environments while maintaining a single Firebase project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.