.env File Best Practices
env Security Configuration Best Practices
.env files hold secrets like API keys and database URLs. Here are best practices to keep them safe.
Never Commit .env
Add .env to .gitignore. Never push secrets to version control.
Use .env.example
Create .env.example with placeholder values (no real secrets). Commit this so others know what variables are needed.
API_KEY=your_api_key_here
DATABASE_URL=postgres://...
Environment-Specific Files
Use .env.local, .env.development, .env.production as needed. Load the right one per environment.
Validation
Validate required env vars at startup. Fail fast if something is missing instead of failing later in production. Use the .env Formatter to format and check for typos.
Summary
Treat .env as secret. Use .env.example for documentation. Validate on startup.