There are the the standard reasons of avoiding committing production secrets in code, like Rabbit R1. However, I’ve come across more projects that do rely on .env files, but simply haven’t documented what the .env should look like, or where to get API keys, database connection strings, etc. Because it’s gitignored, it’s a puzzle to figure out what goes in it.
I’m writing this so I can signpost developers to a simple, maintainable way to set up configuration in their projects. I’d love your feedback would gladly update it if you’ve got better ideas.
Keeps project configuration maintainable. You get a clear error immediately when the app starts, instead of getting confused by weird application or library behaviour because a string was empty later. You have up to date configuration documentation.
Bonus: You can also add additional error messages and documentation when the configuration is wrong or out of date, to help migrate existing developers local .env files when you make changes to it.