Environment variables configure applications across environments. Here's how to manage them securely and effectively.
Basic Usage#
Type-Safe Configuration#
Environment Files#
Loading Environment Variables#
Secrets Management#
Validation Patterns#
Configuration Object#
Docker and Kubernetes#
Best Practices#
Security:
✓ Never commit secrets
✓ Use .env.example for documentation
✓ Use secret managers in production
✓ Rotate secrets regularly
Validation:
✓ Validate on startup
✓ Fail fast on missing required vars
✓ Use type-safe configuration
✓ Provide sensible defaults
Organization:
✓ Use consistent naming (SCREAMING_SNAKE_CASE)
✓ Group related variables
✓ Document all variables
✓ Use environment-specific files
Conclusion#
Environment variables separate configuration from code. Use .env files for local development, secret managers for production, and always validate configuration at startup. Type-safe configuration objects prevent runtime errors and improve developer experience.