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
I have a react app with build folder ( generated by : npm run build ) , I want to have 3 variables in my .env file ( just one file ):
REACT_APP_API_URL=http://localhost:3000/api ( for production)
REACT_APP_PROD_URL=http://localhost:3001/api ( for staging)
REACT_APP_STAGIN_URL=http://localhost:3002/api ( for development)
and use them in the npm run build folder
Create differences env files
install yarn add env-cmd
and add script to package.json:
"start-qa": "env-cmd -f .env.qa react-scripts start"
or build:
"build-qa": "env-cmd -f .env.qa react-scripts build"
try with cross-env
something like this in your package.json
"start": cross-env REACT_APP_API_URL=http://localhost:3000/api node scripts/start.js
may be it will help you in achieving your stuff
You need to create a separate .env file for each environment.
As said in CRA Documentation: What other .env files can be used?:
.env.development, .env.test, .env.production: Environment-specific settings.
You can't place all your env variables in one file, the values will be overridden. For more information check dotenv documentation
Or if you want to edit env variables after building your project, please refer to this: https://stackoverflow.com/a/56120507/5078746
I am trying to have a custom environment variable on build with React, by default it's set to production but I'm trying to have a custom QA build with a custom variable.
Ps : Project is ejected, is there anyway I can manage to do that without messing with my webpack configs.
This is how I do it (I am using react-scripts) :
In package.json I have those scripts :
"build:prototype": "env-cmd -f ./.env.prototype npm run-script build",
"build:demo": "env-cmd -f ./.env.demo npm run-script build",
"build:local": "env-cmd -f ./.env.local npm run-script build",
"build:production": "env-cmd -f .env.production npm run-script build",
and I call them just like any other : npm run build:prototype for example
Hope it helps
Edit: of course I have those .env files at the root of the project
Use webpack's Define Plugin
new webpack.DefinePlugin({
"CUSTOM_VARIABLE": JSON.stringify("whatever you want to set it to")
})
And wherever you need to change certain things in your app, you can check the value of process.env.CUSTOM_VARIABLE and act according to its value. If there are only a few places that depend on this variable, this will work fine. If there are more, it might be worth looking into other approaches, such as React's Context Hook API
I need to create a deploy package. That would after ng build --prod will create SOME_APP.zip file any name I will put in deploy.config.json or anywhere.
There is a way to do it without using webpack-plugins?
this kinda works but probably it's not how package.json was supposed to be used:
"scripts": {
"all": "ng test; ng build --prod; zip -r dist/app.zip dist/app;"
},
in other proj I see ppl relying on Jenkins and/or deploy tools to zip it.
Windows + 7-Zip Version:
Zip in dist-Folder without the folders dist/app-name
File is named according to your app-name and version, leading to a local history of version-builds if something's going wrong..
assuming that outputPath in angular.json is not changed..
"scripts": {
"build": "ng build --prod && cd dist && c:\\\"Program Files\"\\7-Zip\\7z.exe a %npm_package_name%_%npm_package_version%.zip ./%npm_package_name%/*",
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.