Understanding NODE_ENV in Node.js: An Easy Guide

Node.js is a popular JavaScript runtime. It contains a lot of tools that come in handy for developers, one of which is NODE_ENV. This tool helps the Node.js application know where it is being run and how it has to behave. Further in this article, we will get in-depth knowledge about different modes in NODE_ENV, how to set it and many more. Let’s get started with the very basics.

Introduction to ENV

ENV stands for environment variables that are used to store the configurations of settings, system paths, credentials or any values. This includes variables whose values are set outside the program and can be accessed inside our program/code without actually showing their value. It is mostly used to hide secret phrases, DB connection URLs, API keys, etc in a website or on the backend.

Setting Values in a .env File

To add environment variables, follow the below steps:

1. Create an env file named “.env”.

2. Choose a variable name (preferred format is all caps), e.g., NODE_ENV.

3. Assign a value to it after a single equal sign (‘=’), e.g., NODE_ENV=’production’.

Example:

Here’s an example of how the .env would look.

Assigning Values in a .env File

Accessing Values from a .env File

To access a .env value, install the dotenv module and configure it as shown below:

1. Install the dotenv module:

npm install dotenv

2. Configure into the app:

// require dotenv module and configure it with .config() method
require('dotenv').config()

3. Accessing a value:

process.env.VAR_NAME

Example:

require("dotenv").config();

const development_mode = process.env.NODE_ENV
console.log(development_mode);

Output:

Accessing Values from a .env File

Understanding the NODE_ENV Environment Variable

The NODE_ENV is like a secret controller in Node.js applications, which helps developers control how their code behaves.

The NODE_ENV is an environment variable that indicates the environment in which the application is running. Developers widely use it to control various aspects of an application behavior which is based on the stage of the deployment.

The NODE_ENV has 3 modes which are development, production and testing. Node.js assumes it is running in development mode by default.

1. Development Mode: In the development mode the Node.js application works in the development phase in which it is being built step by step. This mode makes it easy to find errors and display detailed information about an error or process. It contains a lot more features that help in making development easier. To set up NODE_ENV to development mode we use NODE_ENV=development.

2. Production Mode: If we set the application in production mode it alerts Node.js that now the application is ready for deployment. In this mode the main focus is on performance, less console logs are made to avoid excessive clutter on the console. This mode helps optimize resource usage. To set up NODE_ENV to production mode we use NODE_ENV=production.

3. Test Mode: After development, we have to run some tests on the application. This involves configuring a testing environment for controlled and reliable testing. To set up NODE_ENV to test mode we use NODE_ENV=test.

Setting Up NODE_ENV

Setting up NODE_ENV can be done easily with the command line or within our code, let us explore both of the ways by which it can be done.

Setting NODE_ENV Using Command Line

Syntax:

For Linux and Mac

export NODE_ENV=development

or

NODE_ENV=development node app.js

For Windows

$env:NODE_ENV='development'

or

set NODE_ENV=development && node app.js

Example:

require("dotenv").config();
const development_mode = process.env.NODE_ENV
console.log(development_mode);

Output:

Setting NODE_ENV Using Command Line

Setting NODE_ENV Within Code

Setting up NODE_ENV in our code can be done by simply adding it to the .env file in our project.

Syntax:

Write the following into a .env file

NODE_ENV=<environment>

or

Write the following into a JavaScript file to be executed

require("dotenv").config();
process.env.NODE_ENV = <environment>

Example:

require("dotenv").config();

process.env.NODE_ENV = process.env.NODE_ENV || "development";

console.log(`Current environment: ${process.env.NODE_ENV}`);


if (process.env.NODE_ENV === "production") {  
  console.log("Running in production mode");
} else {
  console.log("Running in development mode");
}

Output:

NODE_ENV after setting within code

Difference Between NODE_ENV=’environment’ and process.env.NODE_ENV=’development’

NODE_ENV=’environment’ is a static declaration made in the ‘.env’ file, this value is assigned during configuration, as it is set statically it cannot change during runtime, and it will be set as the default value for the NODE_ENV whereas process.env.NODE_ENV=’development’ is a dynamic assignment within our code, this enables us to change the value during runtime on certain conditions.

Conclusion

In this article, we have understood that in Node.js the NODE_ENV works as a guide that helps developers understand the behaviour of their applications. It contains different modes (development, production, test) which help understand the current state of the application and its working. We also went through how to set the NODE_ENV in 2 different ways: with the command line and within the code.

Ready to Explore More? Discover the Differences Between Development and Production Environment in Node.js!

Reference

https://stackoverflow.com/questions/16978256/what-is-node-env-and-how-to-use-it-in-express

Ronak Bhanushali
Ronak Bhanushali
Articles: 4