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

Difference in environment variable usage between development and production modes?

我正在学习如何将一个简单的后端 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 config:set MONGODB_URI=mongodb+srv... 环境变量?第一句话说环境变量只用于开发模式。

我哪里理解错了?是不是开发模式和生产模式都用到了环境变量,我看的note的写法有误?

我认为这是说 .env 中的环境变量将在开发时使用(不是 Heroku),如果你确实想在 Heroku 中使用你的环境变量,你需要这样做通过 heroku config:set ....

What am I understanding wrong? Are environment variables used in both development mode and production mode, and the wording of the note I read was wrong?

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

在生产模式下,Heroku本质上是这样的,例如:

PORT=9999 node index.js

试试吧,你可以通过以下方式在 Node 中访问这个变量:

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

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