The variable process.env.NODE_ENV always bothers me every time I see it in a config file, especially webpack.config.js. Where can I find this? Because sometimes I want to change it's value to production instead of development.
Asked
Active
Viewed 1,456 times
1
Harikrishnan
- 9,688
- 11
- 84
- 127
2 Answers
2
You can set any environmental variable like NODE_ENV while running your applications.
Linux/Mac
NODE_ENV=production node myapp.js
Windows/Powershell
$env:NODE_ENV="production" ; node myapp.js
You can access environmental variable NODE_ENV inside your myapp.js by
process.env.NODE_ENV
Harikrishnan
- 9,688
- 11
- 84
- 127
0
process.env.NODE_ENV is Node.js environment.
With the help of this mostly we decide our deployment environment in which we are deploying the code.
It can be development,stag or production.
We can do other stuff on env, it's up to us.
to set in Linux
export NODE_ENV=production
on Windows
set NODE_ENV=production
Example
config.js
{development:{
//your `env` value
},
production:{
//Your `env` value
}}
Now on the basis of NODE_ENV your env will be set
abdulbarik
- 6,101
- 5
- 38
- 59
-
so you can only set it in command line rather than initialize it in a config file? – Mar 30 '17 at 02:35
-
No, you can set with code as well, for better understand, you can see this link http://stackoverflow.com/questions/29560844/node-js-how-to-set-environment-variables-in-code – abdulbarik Mar 30 '17 at 07:58