Node.js 开发模式和生产模式之间环境变量使用的差异?

Node.js 开发模式和生产模式之间环境变量使用的差异?,node.js,heroku,environment-variables,development-environment,production-environment,Node.js,Heroku,Environment Variables,Development Environment,Production Environment,我正在学习如何将一个简单的backend node.js项目部署到生产环境中,在我的例子中是heroku。我在.env文件中遇到了关于环境变量的注释: The environment variables defined in dotenv will only be used when the backend is not in production mode, i.e. Heroku. We defined the environment variables for development i

我正在学习如何将一个简单的backend node.js项目部署到生产环境中,在我的例子中是heroku。我在.env文件中遇到了关于环境变量的注释:

The environment variables defined in dotenv will only be used when the backend is not in production mode, i.e. Heroku.

We defined the environment variables for development in file .env, but the environment variable that defines the database URL in production should be set to Heroku with the heroku config:set command:

heroku config:set MONGODB_URI=mongodb+srv:...
如果使用heroku意味着我的后端处于生产模式,我的.env文件如何使用
heroku配置:set MONGODB_URI=MONGODB+srv…
环境变量?第一句话说明环境变量仅用于开发模式


我理解错了什么?在开发模式和生产模式中是否都使用了环境变量,而我读到的注释的措词是错误的?

我想它是说
.env
中的环境变量将在开发时使用(而不是Heroku)如果你想在Heroku中使用你的环境变量,你需要通过
Heroku config:set…
来实现

我理解错了什么?是否在开发模式和生产模式中都使用了环境变量,而我读到的注释的措辞是错误的

环境变量用于所有场景,但仅在本地开发模式下,从文件中读取。 是一个允许从文件中读取环境变量的包

在生产模式中,Heroku基本上是这样做的,例如:

PORT=9999 node index.js

试试看,您可以使用以下方法在节点内访问此变量:

console.log(process.env.PORT);
// 9999

环境变量只是可用于整个过程的全局变量。

因此,如果.env文件仅在本地开发模式下读取,那么如果.env文件中存在
heroku config:set MONGODB_URI…
我的应用程序的生产模式如何工作?一旦我部署到Heroku,我的应用程序如何使用
MONGODB_URI
环境变量,因为我的应用程序现在将处于生产模式?因此,在.env文件中使用
Heroku config:set…
将允许在生产模式中使用.env文件中的环境变量?不,不是在.env文件中。我对Heroku不是很熟悉,但我想你会在终端上使用这个命令,Heroku会在他们的服务器上使用这个命令。