NODE_ENV Variable: Understanding and Using Environment Variables in Node.js

beginner
10 min

NODE_ENV Variable: Understanding and Using Environment Variables in Node.js

Node.js, a powerful JavaScript runtime, provides a feature called Environment Variables. These variables help manage configuration settings in different environments like development, testing, and production. In this tutorial, we'll explore the NODE_ENV variable, one of the most commonly used environment variables in Node.js.

What are Environment Variables?

šŸ’” Pro Tip: Environment variables are user-defined values that can be accessed across your entire application. They help separate configuration settings from your code.

Understanding NODE_ENV

The NODE_ENV variable is a built-in environment variable in Node.js. It's used to determine the current environment (development, testing, or production) and adjust the application's behavior accordingly.

Setting NODE_ENV

You can set the NODE_ENV variable in three ways:

  1. Command Line: When starting your Node.js application, you can pass the environment variable as an argument:
bash
node app.js --env=production
  1. Process.env: Inside your Node.js application, you can access and set the NODE_ENV variable using process.env.
javascript
// Set NODE_ENV process.env.NODE_ENV = 'production'; // Access NODE_ENV console.log(process.env.NODE_ENV);
  1. .env File: It's a good practice to store environment variables in a .env file to keep your code clean and separate configuration settings. To use this method, you'll need a package like dotenv.
bash
npm install dotenv

In your .env file:

NODE_ENV=production

In your Node.js application:

javascript
require('dotenv').config(); console.log(process.env.NODE_ENV);

Default and Valid Values

By default, the NODE_ENV variable is set to 'production'. However, it can take on several values:

  • development: The application is running in a development environment.
  • test: The application is running in a testing environment.
  • production: The application is running in a production environment.
  • staging: The application is running in a staging environment.

Using NODE_ENV in Different Environments

Now that you know how to set and access the NODE_ENV variable, let's see how it's used in different environments:

Development

In development, you might want to enable more logging and use development-specific dependencies.

javascript
if (process.env.NODE_ENV === 'development') { console.log('Running in development mode.'); }

Production

In production, you'll typically want to minimize logging, optimize your code, and use production-specific dependencies.

javascript
if (process.env.NODE_ENV === 'production') { console.log('Running in production mode.'); }

šŸ“ Note: It's a best practice to keep sensitive information like API keys and database credentials out of your code and in environment variables.

Quiz Time!

Quick Quiz
Question 1 of 1

What is the default value of the `NODE_ENV` variable in Node.js?

Quick Quiz
Question 1 of 1

What are environment variables used for in Node.js?

With this lesson, you now have a good understanding of the NODE_ENV variable and its role in managing environment-specific settings in Node.js applications. Happy coding! šŸŽ‰