The Wayback Machine - https://web.archive.org/web/20240528022011/https://www.geeksforgeeks.org/how-to-fix-the-node_env-is-not-recognized-error-in-node-js/
Open In App

How to Fix the “NODE_ENV is not recognized” Error in Node JS

Last Updated : 26 Dec, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

We generally come across the error message “NODE_ENV is not recognized as an internal or external command, operable command, or batch file” while trying to set an environment variable in a package.json script in Node JS. This guide will assist you in resolving the issue with a straightforward solution.

When using the following syntax in a Windows environment, it won’t work as expected:

"scripts": {
"test": "NODE_ENV=test mocha",
},

This leads to the error message:

“NODE_ENV” is not recognized as an internal or external command, operable command or batch file.

Example: Below is the code example which generate error.

Javascript




//app.js
const express = require('express');
const app = express();
const port = 3000;
 
// set session in the / route
app.get('/', (req, res) => {
    // session variable
    res.send(`<h1>Hey Geek!</h1>`);
});
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});


Javascript




//package.json
"scripts": {
   "start": "NODE_ENV= node app.js"
 }


Output:

errornodeGIF

Output

To overcome this issue, this is how you set an environment variable in windows:

"scripts": {
"test": "SET NODE_ENV=test & mocha ",
},

After making this adjustment, the “NODE_ENV” error should no longer occur, and your script will execute without any issues.

Example: Below is the code example which generate error.

Javascript




//app.js
const express = require('express');
const app = express();
const port = 3000;
 
// set session in the / route
app.get('/', (req, res) => {
    // session variable
    res.send(`<h1>Hey Geek!</h1>`);
});
app.listen(port, () => {
    console.log(`Server is running on http://localhost:${port}`);
});


Javascript




//package.json
"scripts": {
   "start": "cross-env NODE_ENV=production node app.js"
 }


Output:

errorfreenodeGIF

Output



Similar Reads

How to resolve 'node' is not recognized as an internal or external command error after installing Node.js ?
There are many different ways to install node.js on a computer. The simplest method to verify whether node.js has been properly installed in your computer is simply type node-v in the command prompt or Windows PowerShell. But many times, it happens, most commonly if you're a beginner, the command prompt prints the output something like this: 'node'
2 min read
What is NODE_ENV in Node ?
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
4 min read
NODE_ENV Variables and How to Use Them ?
Introduction: NODE_ENV variables are environment variables that are made popularized by the express framework. The value of this type of variable can be set dynamically depending on the environment(i.e., development/production) the program is running on. The NODE_ENV works like a flag which indicates whether the server is running on development or
2 min read
How to Fix the "partial is not defined" Error in EJS ?
Encountering an error message like "partial is not defined" can be a stumbling block when you're working with EJS (Embedded JavaScript) templates. This error typically pops up when you're trying to use the `partial` function in EJS, but it's not being recognized. Don't worry, though it's a common hiccup for many developers, and we've got you covere
3 min read
How to fix "Error: NextRouter was not mounted" ?
When building applications with Next.js, encountering errors is inevitable. One such error that can be particularly frustrating to deal with is the "Error: NextRouter was not mounted." This error typically occurs when there are issues with how the Next.js router is initialized or utilized within your application. In this guide, we'll explore what c
3 min read
JavaScript for...in loop not working - Object property is not defined Error
A for...in loop is a special loop in JavaScript that enumerates all the keys (object property names) of an object. With each new iteration, a new property string is assigned to the loop variable. Error: Uncaught ReferenceError: Object property is not defined If we try to compare this loop variable with a non-string variable a ReferenceError is gene
3 min read
How To Fix the Error "Metamask stopped injecting Web3"?
Let's see some basic terminologies first as follows: Metamask: Metamask is a cryptocurrency software wallet that interacts with the Ethereum network.web3.js: web3.js is a collection of libraries that allow you to interact with a local or remote Ethereum node using HTTP, IPC, or WebSocket. Blockchain: A blockchain is a digitally distributed, decentr
3 min read
How to fix a 413: request entity too large error ?
In this article, we will learn about an Error request entity too large which typically occurs when the size of the data, being sent to a server, exceeds the maximum limit allowed by the server or the web application in web development when handling file uploads, form submissions, or any other data sent in the request body. We will discuss the follo
4 min read
How to Fix a 401 Unauthorized Error?
Are you seeing a 401 Unauthorized Error and wondering how to fix it? The 401 Unauthorized Error is an HTTP status code error that represents the request sent by the client to the server that lacks valid authentication credentials. It may be represented as "401 Unauthorized", "Authorization required", or "HTTP error 401- Unauthorized". It represents
8 min read
How to Fix GitHub Error: Authentication Failed from the Command Line ?
GitHub is a widely used platform for version control and collaborative development. However, encountering authentication errors can impede your workflow. One common issue is the "Authentication Failed" error, which occurs when your credentials are not properly recognized. This article will guide you through troubleshooting steps to resolve this err
3 min read