'NODE_ENV' is not recognized as an internal or external command, [duplicate] - node.js

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.

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

for windows use & in between command also. Like,
"scripts": {
"start": "SET NODE_ENV=development & nodemon app/app.js",
}

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.

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.

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

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

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.

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]

You can solve this if you're using "Yarn Packager" by the following command:
yarn global add win-node-env

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.
run npm install --save-dev cross-env
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"
},

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

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"

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

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 &

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"

You can use this syntax (using "cross-env") ->
cross-env NODE_ENV=prod node dist/main

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

try using this
NODE_ENV =development node server.js

Related

how does cross-env command works in nodejs?

I have the following line in my package.json
"scripts": {
"start": "cross-env NODE_ENV=development node index.js"
}
I can see that "yarn start" command is running fine, but when I run
"cross-env NODE_ENV=development node index.js" command directly in the terminal, I am getting the following error:
zsh: command not found: cross-env
If cross-env is not registered in the terminal, how does "yarn start" command works?
https://docs.npmjs.com/cli/v7/configuring-npm/folders#executables
When in local mode, executables are linked into ./node_modules/.bin so that they can be made available to scripts run through npm. (For example, so that a test runner will be in the path when you run npm test.)
It's simply a feature to make things easier. It also means if you're working a project with multiple people, you only have to npm install --save a module--you don't have to worry about everyone in your project manually installing it globally. If you wish to run it yourself on the command line, you can either:
Install the module globally
Manually type in the command line ./node_modules/.bin/cross-env

How to run few npm scripts in one-line shell command?

I have three scripts in package.json:
Watch server TypeScript
Nodemon
Webpack
"scripts": {
"watch-server": "tsc --watch --project ./server/tsconfig.json",
"watch-node": "nodemon --watch ./server/build/ --watch ./server/templates -e js,json,pug",
"watch-client": "webpack --config ./webpack/webpack.dev.conf.js --watch"
}
Everytime I start my computer and open VS Code I need to open three separate PowerShell terminals and type in those commands one-by-one. Is there any way to launch these three separate terminals with their own commands in one shell command? Maybe via tasks.json?
On linux or any bash terminal, you can use && to combine multiple commands, i
You can do as
npm run watch-server && npm run watch-node && npm run watch-client
A quick google search for powershell suggested using semicolon
so on powershell you can do something like below if using && does not work
npm run watch-server;npm run watch-node ; npm run watch-client
Also keep in mind, you can additionally add fourth command in your npm scripts in package.json where you can use one of these combined commands which works for you, like
start-all: npm run watch-server && npm run watch-node && npm run watch-client
and then run
npm run start-all

$(pwd) equivalent on windows in node run script

I use the following on my linux machine in node.js in my package.json under the scripts object:
"dev": "NODE_ENV=development NODE_PATH=\"$(pwd)\" browser-refresh --nolazy --inspect=9229 application/index.js",
I know cd is the windows equivalent of pwd, but I don't know how to get it into the node.js run script. I'm not used to window command line at all.
Weird but this works after trying for so long:
"dev_w": "cmd.exe /C \"set NODE_ENV=development && set NODE_PATH=.&& browser-refresh --nolazy --inspect=9229 application/index.js\""

keep webpack dev-server running even after terminal closes

Hi is there a way i can keep webpack devserver running even after closing terminal.
"scripts": {
"dev-server": "npm run templates && webpack-dev-server -d --https --port 28443",
}
when i run npm run dev-server it starts but after closing terminal webpack devserver also closes is there any way to keep it running with pm2 or any other method.
use nohup
So if the script command is "dev-server" (according to your snippet), then go to your project root directory (where the package.json file is, which has your "scripts" section):
If on unix environment, just run nohup npm run dev-server &
If on windows, install git bash - it's sort of a mini unix environment for windows. And then run the nohup npm run dev-server &
This will start the webpack dev server and keep it running on the background
For me, my script section is
"scripts": {
"dev": "webpack-dev-server --open"
}
and the above command that I mentioned works fine
nohup did somehow not work for me, but i was able to make it work with forever.
sudo npm install -g forever
forever start -c "webpack serve" ./

NODE_PATH not recognized

Here is my package.json script:
"scripts": {
"start": "NODE_PATH=$NODE_PATH:./shared node",
"dev": "npm run start & webpack-dev-server --progress --color"
},
When I run npm start in Windows 8 it shows the below error:
node_path is not recognized as a internal or external command, operable program or batch file
I had the same problem when I wanted to set the environment variable in a browserify script:
"scripts": {
"build:symlinked": "NODE_PATH=./node_modules browserify src/index.js > dist/build.js"
}
To be able to use linked node modules that are requiring peer-dependencies.
As mentioned above, you can try to set the environment variable manually or by script where it seems you have to use different commands depending on what command line tool you use.
For not having to do this every time, I found that npm package: cross-env.
By installing it and applying the script like this
"scripts": {
"build:symlinked": "cross-env NODE_PATH=./node_modules browserify src/index.js > dist/build.js"
}
I was able to solve that problem. This is mainly useful, if you work in a team with mixed MAC/Linux and Windows users, so you don't have to to take care about applying the Environment variables in such scripts anymore.
You don't need to define environment variable in package.json just use this
{
"scripts" : "node server.js"
}
or define what you want, here is the reference link.

Resources