Skip to content
Related Articles
Open in App
Not now

Related Articles

What is NODE_ENV in Node.js ?

Improve Article
Save Article
  • Last Updated : 26 Nov, 2022
Improve Article
Save Article

Environment Variables: Before diving into NODE_ENV let us have some context about environment variables. An environment variable is a dynamic-named value that is typically used to provide the ability to configure the value into the code anonymously from outside. Simply speaking, an environment variable is a variable (key-value pair) whose value is set outside the program and its value can be used inside the code anonymously by using some mechanism. These environment variables that were set at the moment the process was started are hosted by the env property provided by the process core module of Node.js. 

Examples of environment variables:


Above environment variables can be accessed like:


The process.env object is a global Node object, and variables are passed as strings. The variable names are all uppercase, with words separated by an underscore is a long followed convention still used.

Now that we have some knowledge about environment variables, let us move forward and know about NODE_ENV.

What is NODE_ENV?

NODE_ENV is an environment variable that stands for node environment in express server. The NODE_ENV environment variable specifies the environment in which an application is running (usually, development or production). Depending on this an application may perform specific tasks like turn debugging on or off, listen on a specific port, etc.

NODE_ENV as performance booster: One of the simplest things we can do to improve performance is to set NODE_ENV to “production”. 

Setting NODE_ENV to “production” makes Express:

  • Cache view templates.
  • Cache CSS files generated from CSS extensions.
  • Generate less verbose error messages.

Thereby improving the performance of the application which is comparatively slower in development.

How to properly set NODE_ENV?

NODE_ENV works like any other environment variable. How to set it depends on the platform being used.

On Linux and OSX: export NODE_ENV=production
On Windows: $env:NODE_ENV = 'production'

Set at the time of starting the application: On all platforms, we can explicitly set the NODE_ENV at the time of starting the application like: 

NODE_ENV=production node app.js

Accessing NODE_ENV: We can access NODE_ENV like any other environment variable using process and env of NodeJS as we have already seen while learning about environment variables.


We can write environment-specific code by checking the value of NODE_ENV by using process.env.NODE_ENV.

Below are a few code snippets for reference on accessing and using NODE_ENV:

Example 1:


var environment = process.env.NODE_ENV;
if(environment === 'production') {
    /* Do something specific
    to production environment. */

Example 2:


var environment = process.env.NODE_ENV;
if(environment === 'development') {
    // connect to local database.
else {
    // connect to hosted database.

Note that:

  • We can use app.get(‘env’) function provided by express to get the value of NODE_ENV but it defaults to “development” rather than production otherwise.
  • If NODE_ENV is not set explicitly for the environment and accessed using process.env then it will be undefined.
  • It is a bad idea to set NODE_ENV from within an application. In such a case, it will only be applied to the process from which it was set.

The node application contains .env file in the root of your project directory and it acts as a hidden file which is used to pass environment variables to the application. It is a secret file that cannot be accessed by anyone except the developer and hence can be used to store private / secret/ hidden data ( data that you cannot afford to make public, i.e., it is vulnerable data), it can be used to store API keys from external services. 

The .env is a shell file, eliminating the need to wrap names or values in quotes. Yet another meticulous rule is that there cannot be space around the equals sign when you are assigning values to the variables, e.g. VAR_NAME=value. Generally, the variable definition is provided on a separate line.

Conclusion: We have pretty much covered all about NODE_ENV in this article. We saw how it is similar to any other environment variable but can be a handy tool when we want to write some environment-specific code or logic. We saw how to set and use NODE_ENV environment variable, its purpose, and its uses. 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!