"NODE_ENV" is not recognized as an internal or external command, operable command or batch file

Viewed 269469

I'm trying to setup an environment for a Node.js app. but I'm getting this error every time.

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

What does this mean and how can I solve this problem?

I'm using Windows and also tried set NODE_ENV=development but had no luck.

27 Answers

I had the same problem and on windows platform and i just ran the below command

npm install -g win-node-env

and everything works normally

Changing your scripts to accommodate Windows is a royal pain. Trying to figure out the appropriate Windows translations and maintaining 2 sets of scripts is no way to live your life.

It's much easier to configure npm to use bash on Windows and your scripts will run as is.

Simply run npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe". Make sure the path to the bash executable is correct for your machine. You'll likely need to start a new instance of the terminal for the change to take effect.

The screenshot below illustrates the benefit.

  1. npm ERR! when trying to run script initially.
  2. Script modified for Windows use runs but doesn't show the return message.
  3. After updating npm config to use bash, the script runs and returns the appropriate message.

Getting npm scripts to run as is in Windows

For those who uses Git Bash and having issues with npm run <script>,

Just set npm to use Git Bash to run scripts

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe" (change the path according to your installation)

And then npm will run scripts with Git Bash, so such usages like NODE_ENV= will work properly.

This worked for me since it's an easy fix. I cloned a repository which was developed in WINDOWS but I am using MACOS.

If you are using windows use SET as prefix:

"scripts": {
    "dev": "SET NODE_ENV=development && nodemon index.js",
  },

But if you are using MacOS remove the SET keyword and use :

"scripts": {
    "dev": "NODE_ENV=development && nodemon index.js",
  },

So in a nutshell

if you are using windows use SET prefix before your run scripts and remove SET from MacOS (probably LINUX also) as shown above.

Do this it will definitely work

"scripts": {
    "start": "SET NODE_ENV=production && node server"
}
NODE_ENV=development & node [your file name here]

or

SET NODE_ENV=development & node [your file name here]
npm install -S cross-env

Worked for me

For windows open git bash and try

NODE_ENV=production node app.js

If anyone else came here like me trying to find a solution for the error:

'env' is not recognized as an internal or external command

The reason I got this is that I was migrating an angular solution from a mac development machine over to a windows 10 desktop. This is how I resolved it.

  1. run npm install --save-dev cross-env

  2. go into my package.json file and change all the script references from env <whatever> to cross-env <whatever>

Then my commands like: npm run start:some_random_environment_var now run fine on Windows 10.

For windows you can do it like

"scripts": {
    "start:prod" : "SET NODE_ENV=production & nodemon app.js",
    "start:dev" : "SET NODE_ENV=development & nodemon app.js"
},

You can solve this if you're using "Yarn Packager" by the following command:

yarn global add win-node-env

Most of the answers up there didn't help me..

What helped me was NODE_ENV=production&& nodemon app/app.js

Take note of the space. Good luck.

set the script "test" inside the "package.json" file :

FOR EXAMPLE:

In Windows; "test": "SET NODE_ENV=test & jest",

In Linux/Mac; "test": "NODE_ENV=test jest",

"set NODE_ENV=production&& nodemon server.js" this one works for me.

process.env.NODE_ENV is adding a white space do this

process.env.NODE_ENV.trim() == 'production'

below code for windows

"start": "SET NODE_ENV=development & nodemon app.js",
"prod": "SET NODE_ENV=production & node app.js"

set NODE_ENV=production& nodemon server.js

& must be joined because if you put space between production and & then NODE_ENV will contain space in last like this 'production ' So just remove space between production and & and add space after &

You can use this syntax (using "cross-env") ->

cross-env NODE_ENV=prod node dist/main

you can use this

"scripts": {
   "start:dev": "nodemon server.js",
   "start:prod": "SET NODE_ENV=production & nodemon 
   server.js"
},

or you can install this

 npm install -g win-node-env

and you can run NODE_ENV without SET

 "start:prod": "NODE_ENV=production nodemon server.js"

On a windows platform

($env:NODE_ENV="environmentName") -and (node file.js)

Kill the terminal( Ctrl + C) then run the file

node file.js

If you are using Powershell as your terminal by any chance, try Using Git-Bash for the same.

NODE_ENV=development node foo.js
Related