Using NODE_ENV with multiple environments in JavaScript projects Using NODE_ENV with multiple environments in JavaScript projects express express

Using NODE_ENV with multiple environments in JavaScript projects


NODE_ENV is used to differentiate between development and production instances. It is not a good idea to run production code without NODE_ENV=production. NODE_ENV=development is usually not that important, because libraries usually just check to see if NODE_ENV !== 'production'. So if you want to have multiple production node environments, or production-like environments, each of them should set NODE_ENV=production. That said, you can definitely set other environment variables to whatever values you desire, and read them back from node at runtime.

A reasonable example would be to have a local staging and production versions of your configuration. In this case, I would recommend having NODE_ENV be just one of the parameters you set up for each environment. For instance, you might want three different databases for each of local, staging and production but set NODE_ENV to development on local, and production for both staging and production.

Since the variables will be shell variables, you will need a way of loading certain environment variables on the target operating system prior to running the server. Modules like https://www.npmjs.com/package/dotenv look promising for this purpose.


NODE_ENV should be set to either development or production in traditional sense.

The reason being, when you're building a front-end application (React, etc), you either build the application in development mode or production mode. For example, in development mode, you will watch for changes and build continuously. In production mode, you minify the code and optimize it for size.

In case of a node server, the NODE_ENV refers to what mode you start your application with. For example, in development mode, you configure your server and install all devDependencies and watch for changes and live reload the server. And in production mode, you only install dependencies and start the server with optimized configuration.

Now talking about different production environments, say staging, pre-live, live, etc, you should use a separate ENV variable for this. Except local, all other environments are considered production environments and your app should be built with and start in production mode in these environments.

You usually load different configurations, say api keys, urls for each environment. These should be differentiated with a separate ENV variable like APP_ENV.

I usually use APP_ENV to differentiate between staging and live environments.

This is how the package.json will look like with different start scripts for different environments

"scripts": {  "start:local": "NODE_ENV=development APP_ENV=local your-start-script",  "start:staging": "NODE_ENV=production APP_ENV=staging your-start-script",  "start:live": "NODE_ENV=production APP_ENV=live your-start-script",}

You will need to start the app with the right start script in each environment.


You can use NODE_ENV for multiple environments when your install the custom-env module:npm install custom-env Full docs here: https://www.npmjs.com/package/custom-env