为什么不应该检查dotenv文件?(Why dotenv files should not be checked in?)

我是nodejs的新手 ,所以请原谅我,如果我的问题太琐碎了。 我正在使用dotenv创建一个环境文件。

但是,在他们的WebSite上 ,他们建议不要检入.env文件。 所以,我想知道这些文件是否未签入,我将如何告诉其他开发人员应用程序需要的环境变量。 他们需要填写环境变量的 ,因为它可以在js (服务器,配置)文件的所有位置使用。 查看使用env变量并替换的每个文件都不是一件容易的事吗? 除非我完全错过了什么。

任何帮助深表感谢。

I'm new to nodejs, so forgive me if my question is too trivial. I'm creating an enviornment files using dotenv.

However, on their WebSite, they recommend against checking in .env file. So, I was wondering if this files is not checked in , how will I tell the other developers about the environment variables that the application needs. They would require to fill out the values of environment variables because it could be used in all the places in the js (sever, config) files. And it would be non-trivial to look at every files that uses env variables and replace? Unless I'm missing something entirely.

Any help is much appreciated.

最满意答案

环境变量通常用于特定于环境的配置值,如数据库凭据,API端点等。 由于它们是特定于环境的,并且通常包含数据库凭据等敏感数据, .env不应提交.env文件。

如果要显示使用哪些环境变量,则一种方法是创建和提交样本文件:

.env.sample

DB_HOST=localhost DB_USERNAME= DB_PASSWORD= DB_DATABASE=our_project

然后由其他开发人员复制相同的内容并创建他们自己的.env文件(或者只是填充他们系统上的相关环境变量)。

Environment variables are typically used for environment-specific configuration values, like database credentials, API endpoints, and so on. Since they're environment-specific, and usually hold sensitive data like database credentials, .env files should not be committed.

If you want to show which environment variables are used, one method is to create and commit a sample file:

.env.sample

DB_HOST=localhost DB_USERNAME= DB_PASSWORD= DB_DATABASE=our_project

Then it's up to the other developers to copy the same and create their own .env file (or just populate the relevant environment variables on their system).

更多推荐