简体   繁体   English

process.env.NODE_ENV 是否自动设置为“生产”?

[英]does process.env.NODE_ENV sets to 'production' automatically?

I was reading someone's code and came across this line.我正在阅读某人的代码并遇到了这一行。

if (process.env.NODE_ENV === 'production') { ...

This is a node.js application with express server and reactjs front end.这是一个带有快速服务器和 reactjs 前端的 node.js 应用程序。 So lets say we deploy it to heroku will it get this NODE_ENV variable automatically(or set by heroku itself) or do I have to set it manually as we set other env variables in development server?因此,假设我们将它部署到 heroku,它会自动获得这个 NODE_ENV 变量(或由 heroku 本身设置)还是我必须在我们在开发服务器中设置其他环境变量时手动设置它?

如此处所述,如果您将 NODE_ENV 部署在 Heroku 上,则默认设置为生产: https ://devcenter.heroku.com/articles/nodejs-support#runtime-behavior

process.env refers to the environment variables on the system. process.env 指的是系统上的环境变量。 This depends a whole lot on where the application runs and how the system is configured.这在很大程度上取决于应用程序运行的位置以及系统的配置方式。 Herocku does not know if a specific version of your app is in test or production. Herocku 不知道您的应用程序的特定版本是在测试中还是在生产中。 So the developer has defined an environment and added 'production' as a variable in that environment.因此,开发人员定义了一个环境并在该环境中添加了“生产”作为变量。

If you use npm you can add variables with script command如果您使用 npm,您可以使用脚本命令添加变量

{
  ..
  "scripts": {
    "dev": "corss-env ENVIRONMENT=development ..",
    "prod": "cross-env ENVIRONMENT=production .."
  }
  ..
}

and use the appropriate command as run according to setting.并根据设置使用适当的命令运行。

https://www.npmjs.com/package/cross-env https://www.npmjs.com/package/cross-env

Inside in your project in Heroku, you can set all environments variables.在 Heroku 的项目中,您可以设置所有环境变量。 Go in your project > settings > Config Vars and set NODE_ENV production.进入您的项目 > 设置 > 配置变量并设置 NODE_ENV 生产。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM