krozero
krozero

Reputation: 6433

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

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.

Upvotes: 427

Views: 399210

Answers (28)

Flion
Flion

Reputation: 10902

If you do this in windows (which works on linux)

set NODE_ENV=production & nodemon app/app.js

it will cause NODE_ENV to contain a space at the end:

process.env.NODE_ENV == 'production'; //false
process.env.NODE_ENV == 'production '; //true

As mentioned in a comment here, use this instead:

NODE_ENV=production&& nodemon app/app.js

Upvotes: 47

Shah Fahad
Shah Fahad

Reputation: 207

For linux environment:

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

For windows environemnt:

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

Upvotes: 1

Jim O'Neil
Jim O'Neil

Reputation: 23764

It sounds like your error comes from an attempt to run something like this (which works in Linux):

NODE_ENV=development node foo.js

the equivalent in Windows would be:

SET NODE_ENV=development & node foo.js

running in the same command shell. You mentioned SET NODE_ENV did not work, but was not clear how/when you executed it.

Upvotes: 438

Rafi
Rafi

Reputation: 105

"scripts": {
"start": "SET NODE_ENV=staging&nodemon index",
"production": "SET NODE_ENV=production&nodemon index"}

in package.json. Don't give space between the env name and nodemon

Upvotes: 0

yash dharia
yash dharia

Reputation: 61

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 &

Upvotes: 2

cxkeeley
cxkeeley

Reputation: 269

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"

Upvotes: 9

Matek
Matek

Reputation: 48

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

cross-env NODE_ENV=prod node dist/main

Upvotes: 2

kawcher578
kawcher578

Reputation: 11

below code for windows

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

Upvotes: 1

Md Moshiur Rahman
Md Moshiur Rahman

Reputation: 71

NODE_ENV=development & node [your file name here]

or

SET NODE_ENV=development & node [your file name here]

Upvotes: 7

Crux_codes
Crux_codes

Reputation: 9

On a windows platform

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

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

node file.js

Upvotes: 0

user14512746
user14512746

Reputation: 75

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

Upvotes: 1

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

yarn global add win-node-env

Upvotes: 6

Ishan Kesharwani
Ishan Kesharwani

Reputation: 292

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.

Upvotes: 9

ElderSam
ElderSam

Reputation: 79

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",

Upvotes: 2

Rohit Goel
Rohit Goel

Reputation: 3554

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

Upvotes: 55

Susan-stack
Susan-stack

Reputation: 2001

  1. npm install --save-dev "cross-env" module.
  2. modify the code as cross-env NODE_ENV=development node foo.js. Then you can run the like npm run build.

Upvotes: 200

jmfirestone
jmfirestone

Reputation: 71

Do this it will definitely work

"scripts": {
    "start": "SET NODE_ENV=production && node server"
}

Upvotes: 7

Husain Dhariwala
Husain Dhariwala

Reputation: 39

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"
},

Upvotes: 3

Hamza Iftikhar
Hamza Iftikhar

Reputation: 582

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

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

Upvotes: 1

Jon Crowell
Jon Crowell

Reputation: 22338

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

Upvotes: 39

user3790180
user3790180

Reputation: 443

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.

Upvotes: 17

Post Impatica
Post Impatica

Reputation: 16393

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.

Upvotes: 4

Nikhil Arya
Nikhil Arya

Reputation: 49

For windows open git bash and try

NODE_ENV=production node app.js

Upvotes: 3

SirPhemmiey
SirPhemmiey

Reputation: 641

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.

Upvotes: 3

Bang Andre
Bang Andre

Reputation: 541

npm install -S cross-env

Worked for me

Upvotes: 4

AmerllicA
AmerllicA

Reputation: 32512

Use win-node-env, For using it just run below command on your cmd or power shell or git bash:

npm install -g win-node-env

After it everything is like Linux.

Upvotes: 74

laggingreflex
laggingreflex

Reputation: 34627

I wrote a module for this: win-node-env.

It creates a NODE_ENV.cmd that sets the NODE_ENV environment variable and spawns a child process with the rest of the command and its args.

Just install it (globally), and run your npm script commands, it should automatically make them work.

npm install -g win-node-env

Upvotes: 652

Mahmudul Hasan
Mahmudul Hasan

Reputation: 3069

for windows use & in between command also. Like,

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

Upvotes: 297

Related Questions