At top of my app.js file I put
NODE_ENV=\'development\';
but I get error that NODE_ENV is not defined. But in the nodejs documentation is says
NODE_ENV is an environment variable.
You set it in your shell when you invoke Node.js.
However, development is the default; you only need to do anything if you want prod.
It is better to start your app in dev mode like this:
NODE_ENV=development node app.js
But if you really wanted to set it your app file just set it like this:
process.env.NODE_ENV= "development"
If you want to set an environment variable in your js file you should do it this way:
process.env.NODE_ENV = 'development';
Alternatively you can set the variable in your shell and run your application:
$ NODE_ENV="development" node ./app.js
or export the variable and run your application:
$ export NODE_ENV="development"
$ node ./app.js
On Windows:
$ set NODE_ENV="development"
$ node app.js
By using NPM you might to be used the follow scripts in the package.json:
"scripts": {
"start": "nodemon ./bin/www",
"dev_win": "set NODE_ENV=development && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log",
"prod_win": "set NODE_ENV=production && node ./bin/www >> /romba/log/api.log 2>> /romba/log/error.log"
"prod_nix": "NODE_ENV=production node ./bin/www >> /romba/log/api.log 2>> /romba/log/_error.log"
},...
To start one of the script use the command:
npm run-script prod_win
In the JavaScript code I check the condition:
process.env.NODE_ENV.indexOf('production') > -1
In order to start from cmd you can try
NODE_ENV=development node yourappname.js
If you are doing this in server where forever is installed you can mention the environment variable like
NODE_ENV=development forever start yourappname.js