.env File vs Config File
People think a .env file is just one more config file, and it is not. A config file holds settings that are the same for everyone, so it goes into Git with the code. A .env file holds the secrets and values for one machine. It must stay out of Git, and each machine keeps its own copy.
.env File
Secret keys and passwords in a file. It stays out of the code and Git.
A .env file holds secrets as simple lines, like API_KEY=abc123. A program loads them as environment variables when it starts. This keeps passwords and keys out of the code itself. Each machine has its own .env, with its own values. It should always be listed in .gitignore so it is never committed.
Configuration File
Settings a program reads when it starts. They sit in a file apart from the code.
A configuration file holds the settings a program reads when it starts. It sets things like the port to use, the database to connect to or which features are on. Changing the file changes how the program acts, with no change to its code. Common formats are YAML, JSON, TOML and INI. Most projects have several, one for each tool they use.
In one line
- .env File
- Secret keys and passwords in a file. It stays out of the code and Git.
- Configuration File
- Settings a program reads when it starts. They sit in a file apart from the code.