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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:03:01  来源:igfitidea点击:

How to start node app with development flag?

node.js

提问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

回答by Hassan Nomani

In order to start from cmd you can try

为了从 cmd 开始,您可以尝试

NODE_ENV=development node yourappname.js

If you are doing this in server where foreveris installed you can mention the environment variable like

如果您在安装了永久的服务器中执行此操作,则可以提及环境变量,例如

NODE_ENV=development forever start yourappname.js