node.js 如何使用开发标志启动节点应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19503641/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to start node app with development flag?
提问by bejm
At top of my app.js file I put
在我的 app.js 文件的顶部,我放了
NODE_ENV='development';
but I get error that NODE_ENV is not defined. But in the nodejs documentation is says NODE_ENV is global. How can I start my app with development settings? Thank you.
但我收到未定义 NODE_ENV 的错误。但是在 nodejs 文档中说 NODE_ENV 是全局的。如何使用开发设置启动我的应用程序?谢谢你。
回答by Errol Fitzgerald
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"
回答by SLaks
NODE_ENV is an environment variable.
You set it in your shell when you invoke Node.js.
NODE_ENV 是一个环境变量。
您在调用 Node.js 时在 shell 中设置它。
However, development is the default; you only need to do anything if you want prod.
但是,开发是默认的;如果你想要生产,你只需要做任何事情。
回答by fardjad
If you want to set an environment variable in your jsfile you should do it this way:
如果你想在你的js文件中设置一个环境变量,你应该这样做:
process.env.NODE_ENV = 'development';
Alternatively you can set the variable in your shell and run your application:
或者,您可以在 shell 中设置变量并运行您的应用程序:
$ NODE_ENV="development" node ./app.js
or export the variable and run your application:
或导出变量并运行您的应用程序:
$ export NODE_ENV="development"
$ node ./app.js
On Windows:
在 Windows 上:
$ set NODE_ENV="development"
$ node app.js
回答by Pax Beach
By using NPM you might to be used the follow scripts in the package.json:
通过使用 NPM,您可能会在 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:
在 JavaScript 代码中,我检查了条件:
process.env.NODE_ENV.indexOf('production') > -1

