process.env.NODE_ENV is undefined issue [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 5, 2024
5 min

banner

# Table of Contents

  1. process.env.NODE_ENV is undefined issue
  2. Using the cross-env module to set the NODE_ENV environment variable
  3. Using the dotenv module to set the NODE_ENV environment variable
  4. Linux and macOS: Setting the NODE_ENV environment variable in your profile file
  5. Windows: Setting the NODE_ENV environment variable permanently
  6. Notes on solving the issue with undefined environment variables

# process.env.NODE_ENV is undefined issue [Solved]

The process.env.NODE_ENV environment variable is undefined when it hasn't been set in the environment.

Regardless if you are trying to access the environment variable in Node.js, in a React or a Vue application, you have to set the environment variable before starting your server.

If you are on macOS or Linux, issue the following command to set the environment variable.

shell
# on macOS and Linux export NODE_ENV=development

set-node-env-variable-on-macos-and-linux

If you are on Windows, run the following command in CMD.

cmd
# on Windows, CMD SET NODE_ENV=development

set node env environment variable windows cmd

If you are on Windows and use PowerShell, use the following command instead.

PowerShell
# on Windows, PowerShell $env:NODE_ENV="development"

set node env environment variable windows powershell

Once you set the environment variable, you will be able to access it as follows.

index.js
console.log(process.env.NODE_ENV);

If I run the code from the terminal in which I set the environment variable, I get the development value back.

print process env node env environment variable

When you use this approach to set the environment variable, it is only set for the current shell session.

You have to rerun the command if you open a new shell session or restart your computer.

If you want to specify a default value if the environment variable is not set, use the logical OR (||) operator.

index.js
const environment = process.env.NODE_ENV || 'development'; console.log(environment);

If you need to check if the environment variable is set to a specific value, use an if statement.

index.js
if (process.env.NODE_ENV === 'development') { // development-only code below }
The code for this article is available on GitHub

If you aren't able to read environment variables in a React.js application, check out the following article:

# Using the cross-env module to set the NODE_ENV environment variable

You can also use the cross-env module to set the NODE_ENV environment variable.

The module enables you to set an environment variable in a universal way that works on all operating systems.

First, install the module by running the following command.

shell
# ๐Ÿ‘‡๏ธ with NPM npm install cross-env # ๐Ÿ‘‡๏ธ with YARN yarn add cross-env

Now, set the command as a script in your package.json file.

The following example sets the NODE_ENV environment variable to development and issues the node index.js command.

package.json
{ "scripts": { "start": "cross-env NODE_ENV=development node index.js" } }
The code for this article is available on GitHub

Make sure to update the value of the environment variable and the command that starts your server.

For example, if your main .js file is called app.js, replace index.js with app.js in the command.

Now if I issue the npm run start command, the NODE_ENV environment variable will be set to development.

npm run start node env set to development

Hardcoding operating system-specific code to set environment variables in package.json is discouraged, especially if you collaborate with other developers.

The crosss-env module works on all operating systems.

The module simply sets the environment variable before starting your server.

# Using the dotenv module to set the NODE_ENV environment variable

If the issue persists and you use Node.js, you can install the dotenv module to set the NODE_ENV environment variable.

  1. Open your terminal in your project's root directory and run the following command.
shell
# with NPM npm install dotenv # with YARN yarn add dotenv
The code for this article is available on GitHub
  1. Create a .env file in the root directory of your project (right next to your package.json file) and set the NODE_ENV environment variable.
.env
NODE_ENV="development"

set node env environment variable in env file

At the top of your entry .js file (e.g. app.js or index.js), add the following code to initialize the dotenv module.

index.js
require('dotenv').config() // ๐Ÿ‘‡๏ธ confirm the NODE_ENV environment variable is set console.log(process.env) console.log(process.env.NODE_ENV); // your other code below

If you use ES6 imports/export use the following syntax instead.

index.js
import * as dotenv from 'dotenv' dotenv.config() console.log(process.env.NODE_ENV); // your other code below
The code for this article is available on GitHub

Make sure to add the import statement and the dotenv.config() call at the top of your entry .js file.

If you try to access environment variables before calling dotenv.config(), they will be undefined.

# Linux and macOS: Setting the NODE_ENV environment variable in your profile file

If you want to permanently set the NODE_ENV or other environment variables on macOS and Linux, you can add them to your profile file.

For example, if you use bash, you can add the variables to ~/.bashrc or ~/.bash_profile.

bash
# with `gedit` sudo gedit ~/.bashrc # or with `nano` sudo nano ~/.bashrc

Add the following line at the bottom of your ~/.bashrc file.

~/.bashrc
export NODE_ENV=development
The code for this article is available on GitHub

The same approach can be used to set any other environment variables. Save the changes and close the file.

Then, source the updated ~/.bashrc file and restart your server.

shell
# BASH source ~/.bashrc source ~/.bash_profile

If you use zsh, you have to add the environment variable to your ~/.zshrc file instead.

shell
sudo gedit ~/.zshrc sudo nano ~/.zshrc

Paste the following line at the bottom of your ~/.zshrc file.

~/.zshrc
export NODE_ENV=development

Source your ~/.zshrc file and restart your server.

shell
source ~/.zshrc

Note that this approach is not recommended when collaborating with other developers.

Instead, you should use the dotenv package or cross-env as shown in the previous subheadings.

The code for this article is available on GitHub

# Windows: Setting the NODE_ENV environment variable permanently

If you want to set the NODE_ENV environment variable or other environment variables permanently on Windows:

  1. Click on the Search bar and type "environment variables".
  2. Click on "Edit the system environment variables".

edit system environment variables

  1. Click on the "Environment Variables" button.

  2. In the "System variables" section, click on the New... button.

  3. Set NODE_ENV as the name of the variable and set its value (e.g. development).

  4. Click on OK to apply the changes and restart your terminal and development server.

# Notes on solving the issue with undefined environment variables

  1. If you get the issue in a React.js application, check out the following article:
  1. If you get the issue in a Node.js application when using the dotenv module, you have to make sure to run the dotenv.config() line at the top of your file.
index.js
require('dotenv').config() // ๐Ÿ‘‡๏ธ confirm the NODE_ENV environment variable is set console.log(process.env) console.log(process.env.NODE_ENV); // your other code below

If you use ES6 imports/export use the following syntax instead.

index.js
import * as dotenv from 'dotenv' dotenv.config() console.log(process.env.NODE_ENV); // your other code below
The code for this article is available on GitHub

If you run the config() method after importing a module that tries to access environment variables, the environment variables will be undefined.

  1. Make sure you haven't misspelled the environment variables that you're trying to set.

  2. Restart your development server. Your dev server reads your .env file only on boot.

The server doesn't watch for changes in your .env file, so if you make any changes to the file, you have to restart it.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev