ChatGPT解决这个技术问题 Extra ChatGPT

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


l
laggingreflex

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

It works! And I didn't have to change any commands. This is the answer.
this is the easiest aswer
How can I add custom variables to you script?
@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.
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.
J
Jim O'Neil

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.


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.
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.
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.
upvoted Susan's answer - original response predated the cross-env module
@krozero try installing all packages again that might help you
M
Mahmudul Hasan

for windows use & in between command also. Like,

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

The command works but the value of NODE_ENV will be 'development ' (the white space between 't' and '&' will be contained by NODE_ENV)
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?
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.
This does exactly what you want if you're trying to run npm start to set the production mode to production.
This is actual a better answer than the accepted one. I didn't face any issues like the above comments :)
M
Matthieu Riegler

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


Please don't forget to include cross-env to the dependencies in package.json
cross-env best answer!
The library is here: 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!
This should probably be the accepted answer. It's a platform agnostic solution
Agreed. This answer covers any env variables and not just node env variables. Best answer!
A
AmerllicA

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.


R
Rohit Goel

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


Worked like a charm, needed this for a project where we are developing across multiple platforms (Mac and Windows).
What does this do exactly?
F
Flion
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

This has been a life saver!
You're the man, worked perfectly
set NODE_ENV=production & nodemon app/app.js perfact
J
Jon Crowell

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.

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

https://i.stack.imgur.com/IhYmc.png


YES! Just what I needed! Who wants to npm install -g more-cr*p anyway?
u
user3790180

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.


I
Ishan Kesharwani

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.
in mac it worked without && what worked for me is NODE_ENV=development nodemon app.mjs
j
jmfirestone

Do this it will definitely work

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

N
Nimantha
NODE_ENV=development & node [your file name here]

or

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

C
CertainPerformance
npm install -S cross-env

Worked for me


b
barbsan

For windows open git bash and try

NODE_ENV=production node app.js


It does work in Git Bash (mintty) when used directly. But when I run the same command from npm <scripts_entry>, I get an error with different phrasing but equivalent meaning: it treats env var name as an executable.
@AndreyMikhaylov-lolmaus npm run <script> uses Windows cmd as default to run commands. You can set it to use Git Bash. npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe" And then npm run will use Git Bash to run scripts.
P
Post Impatica

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.

run npm install --save-dev cross-env go into my package.json file and change all the script references from env to cross-env

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


For Windows users to just switch to Bash is a bit mush, especially when the rest just works. This is a good solution that worked for me.
This should have been the accepted answer. cross-env works no matter what OS you are on.
H
Husain Dhariwala

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

S
SirPhemmiey

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.


This worked for me in Windows 10, Node v14.18.1.
E
ElderSam

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


N
Nimantha

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

yarn global add win-node-env

u
user14512746

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


How does this differ from the other 21 answers already here?
H
Hamza Iftikhar

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

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

k
kawcher578

below code for windows

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

C
Crux_codes

On a windows platform

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

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

node file.js


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.
Y
Yousuf Ejaz

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

NODE_ENV=development node foo.js

y
yash dharia

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 &


T
Tyler2P

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

cross-env NODE_ENV=prod node dist/main