400

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.

Pavindu
  • 2,684
  • 6
  • 44
  • 77
krozero
  • 5,929
  • 3
  • 21
  • 33

30 Answers30

603

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
laggingreflex
  • 32,948
  • 35
  • 141
  • 196
  • 39
    It works! And I didn't have to change any commands. This is the answer. – Abhimanyu Pathania Jan 20 '17 at 22:41
  • 10
    this is the easiest aswer – mrTurkay May 03 '17 at 21:28
  • 1
    How can I add custom variables to you script? – ivan-ivory Jun 12 '17 at 05:23
  • 1
    @ivan-ivory The first variable (i.e. NODE_ENV) has to stay the same (otherwise it'll have to be an entirely separate script). And as for adding custom variables after it (i.e. NODE_ENV=dev SOME_VAR=val) I'll have to modify the logic of the script to parse more variables from process.argv. I've been thinking about it but don't have the time. Feel free to make a pull request. – laggingreflex Jun 12 '17 at 12:54
  • 2
    Didn't work for me. I'm getting: > NODE_ENV=development node_modules/.bin/nodemon --ignore ./public/tones/ --exec babel-node server/index.js 'node_modules' is not recognized as an internal or external command, operable program or batch file. – Rod Lima Mar 22 '18 at 13:53
  • 1
    @RodrigoGarcia Try just `NODE_ENV=development nodemon --ignore ./public/tones/ --exec babel-node server/index.js` ("node_modules/.bin" is added to the path automatically when executing any npm script) – laggingreflex Mar 22 '18 at 15:14
  • 1
    @ivan-ivory I've just added support for custom variables (as long as they come *after* the first variable, as mentioned before), as well as some more commonly used variables (like `ENV`, `DEBUG`, `PORT`), and paved the way to add more in future easily if required. Please check out the latest version. – laggingreflex Sep 12 '18 at 09:34
  • thank you, without this, we wouldn't had done the project – Billy Jan 01 '21 at 09:25
  • Good documented answer – timber535 Apr 05 '21 at 19:03
  • 1
    perfect one. but i would be better if you discussed what are you doing there – Ahmed Khashaba Apr 19 '21 at 22:54
  • This is a good approach if you cannot change the source repo, however if you're creating a package that others are likely to use and want to make it cross-platform, you should use [Susan's answer here](https://stackoverflow.com/a/37131308/195835). – Simon East May 04 '21 at 05:18
  • Superb answer. I can recommend it. – Ezrqn Kemboi Jul 05 '21 at 08:48
  • Didn't work `Note: This command was run via npm module 'win-node-env'` `Error: No command was given` – PwrSrg Jan 14 '22 at 20:46
  • @PwrSrg Could you [file an issue](https://github.com/laggingreflex/win-node-env/issues) with more details please? I'd love to look into it. "No command given" to me seems like you're just running `NODE_ENV=development` without any succeding command like `nodemon`, is that the case at all? – laggingreflex Jan 15 '22 at 16:22
410

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 wasn't clear how/when you executed it.

Jim O'Neil
  • 23,344
  • 7
  • 42
  • 67
  • thanks Jim, i used it in command line and removed from package.json file. but i ran into another after that. looks like node modules are not fully supported by windows. got another error with bcrypt and gyp. – krozero Aug 13 '12 at 22:54
  • not a expert here, but bcrypt shows support for windows, but does require openSSL, not sure if that helps. If not, might want to post a new question since the scenario has changed a bit. – Jim O'Neil Aug 14 '12 at 03:41
  • 12
    While this is indeed working, I think @Susan-stack gave the correct answer - a cross platform solution and not changing the line to work on windows but break other OS. – justabuzz Jun 03 '16 at 12:45
  • upvoted Susan's answer - original response predated the cross-env module – Jim O'Neil Jun 04 '16 at 11:41
  • @krozero try installing all packages again that might help you – Hanzla Habib Jul 10 '20 at 05:03
  • @JimO'Neil, okay so it seems that in a Windows machine you cannot set `NODE_ENV=test` or `NODE_ENV=development` inside of the `"scripts"` section of a `package.json` file, is that about the size of it? – Daniel Nov 02 '20 at 20:54
  • @justabuzz I like to keep external dependencies to minimum and I know what's the only OS in use. this is the answer for me. – Yigal Oct 24 '21 at 08:40
  • The correct answer is https://stackoverflow.com/a/33755445/3728901 , use `&` , not separating in 2 lines. – Vy Do Dec 20 '21 at 23:02
285

for windows use & in between command also. Like,

  "scripts": {
    "start": "SET NODE_ENV=development & nodemon app/app.js",
  }
Mahmudul Hasan
  • 2,949
  • 1
  • 9
  • 9
  • 11
    The command works but the value of `NODE_ENV` will be 'development ' (the white space between 't' and '&' will be contained by `NODE_ENV`) – roroinpho21 Jun 23 '17 at 07:11
  • 1
    exactly what @roroinpho21 says. now I have to `.trim()` the value later to make `process.env.NODE_ENV == 'production'` work. Anyway to avoid this in a oneliner? – Flion Jul 27 '17 at 08:25
  • People who couldn't make it work, `"test-unit": "SET NODE_ENV=test & mocha --require co-mocha 'test.js'"` **wrong** `"test-unit": "SET NODE_ENV=test & mocha --require co-mocha test.js"` **true**. _You need to remove the_ `' '` _around the js file._ – Serhat Türkman Dec 14 '18 at 13:15
  • This does exactly what you want if you're trying to run npm start to set the production mode to production. – Jason Feb 01 '20 at 21:08
  • This is actual a better answer than the accepted one. I didn't face any issues like the above comments :) – sanky May 29 '21 at 08:31
  • SET REACT_APP_STAGE=prod&SET GENERATE_SOURCEMAP=false&react-scripts build worked for me – Alex Mar 25 '22 at 12:39
  • `NODE_ENV=dev&nodemon src/index.js` – Arqam Rafay May 01 '23 at 07:04
  • muy util y simple, gracias. para mi esta es la mejor respuesta! – Carlos Andrés Alzate Aug 30 '23 at 21:38
188
  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.
Matthieu Riegler
  • 31,918
  • 20
  • 95
  • 134
Susan-stack
  • 1,881
  • 1
  • 9
  • 2
  • 14
    Please don't forget to include `cross-env` to the dependencies in `package.json` – Aminah Nuraini Aug 22 '16 at 00:51
  • 4
    cross-env best answer! – Williaan Lopes Sep 25 '19 at 20:32
  • 2
    The library is here: https://github.com/kentcdodds/cross-env -- and that page says to include cross-dev in devDependencies ```npm install --save-dev cross-env```; this also helps with the error ```'env' is not recognized as an internal or external command``` when the npm script said ```env VARNAME=varvalue && ...``` (just remove env and insert cross-env instead). No need for developers to install something globally or to have different npm scripts for different platforms! – Marcus Apr 27 '20 at 23:28
  • 4
    This should probably be the accepted answer. It's a platform agnostic solution – goonerify Mar 12 '21 at 09:04
  • 1
    Agreed. This answer covers any env variables and not just node env variables. Best answer! – Phillip Havea Apr 05 '21 at 03:18
  • Of course, if you want to use `cross-env` in a production startup context, then that package needs to be in the `dependencies`, not `devDependencies`... – ErikE Mar 07 '23 at 00:02
72

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.

AmerllicA
  • 29,059
  • 15
  • 130
  • 154
51

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

Rohit Goel
  • 3,396
  • 8
  • 56
  • 107
42
set NODE_ENV=production & nodemon app/app.js

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
Flion
  • 10,468
  • 13
  • 48
  • 68
36

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

Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
  • YES! Just what I needed! Who wants to `npm install -g more-cr*p` anyway? – Steven de Salas Jun 07 '22 at 09:09
  • This should be the accepted answer. Using git bash for windows is SO much better than `cmd`. There are definitely other options such as directly messing with cygwin or using WSL, but so many node.js scripts want to use shell, that this just MAKES SENSE. Also, [yarn has a similar config option](https://stackoverflow.com/a/58188828/57611). – ErikE Mar 07 '23 at 00:08
16

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.

user3790180
  • 433
  • 5
  • 12
9

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.

  • @Ishan_Kesharwani space is important before && in windows environment and in this example NODE_ENV is "development ", to remove space after development string, statements in dev should be separate with just && without space. – M_Farahmand Nov 24 '21 at 12:09
  • 1
    in mac it worked without `&&` what worked for me is `NODE_ENV=development nodemon app.mjs` – Pooya Estakhri Mar 24 '22 at 01:10
8

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"
cxkeeley
  • 209
  • 3
  • 3
7

Do this it will definitely work

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

or

SET NODE_ENV=development & node [your file name here]
Nimantha
  • 6,405
  • 6
  • 28
  • 69
6

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

yarn global add win-node-env
Nimantha
  • 6,405
  • 6
  • 28
  • 69
4
npm install -S cross-env

Worked for me

CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
Bang Andre
  • 471
  • 7
  • 11
4

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.

Post Impatica
  • 14,999
  • 9
  • 67
  • 78
3

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.

SirPhemmiey
  • 585
  • 6
  • 7
3

For windows open git bash and try

NODE_ENV=production node app.js

barbsan
  • 3,418
  • 11
  • 21
  • 28
  • 1
    It does work in Git Bash (mintty) when used directly. But when I run the same command from `npm `, I get an error with different phrasing but equivalent meaning: it treats env var name as an executable. – Andrey Mikhaylov - lolmaus Dec 21 '18 at 09:41
  • 3
    @AndreyMikhaylov-lolmaus `npm run – user3790180 Apr 11 '19 at 19:05
3

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

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

ElderSam
  • 69
  • 4
2

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

user14512746
  • 83
  • 1
  • 2
2

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 &

1

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

process.env.NODE_ENV.trim() == 'production'
Hamza Iftikhar
  • 584
  • 6
  • 18
1

below code for windows

"start": "SET NODE_ENV=development & nodemon app.js",
"prod": "SET NODE_ENV=production & node app.js"
kawcher578
  • 11
  • 2
1

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

cross-env NODE_ENV=prod node dist/main
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Matek
  • 13
  • 4
0

On a windows platform

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

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

node file.js

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 06 '22 at 01:07
0

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

NODE_ENV=development node foo.js
0

try using this

NODE_ENV =development node server.js

Monjiro
  • 11
  • 2
0
"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

Rafi
  • 105
  • 2
  • 7
0

For those of you running into this with npm scripts on windows, even though you are running your scripts in git bash, npm is still using cmd as its script shell.

Set git bash as npm's script shell and you are good to go.

See my answer here:

https://stackoverflow.com/a/76713342/10547719

noahmason
  • 113
  • 5