Loading Environment Variables
Learn how to manage environment variables across different environments and deployment scenarios
#Local Development
#Using .env
Files
The most common way to manage environment variables during development is using .env
files:
#Best Practices for .env
Files
-
Document Required Variables Create a
.env.example
file to document required variables:.env.example -
Gitignore Configuration Add these patterns to your
.gitignore
:.gitignore -
Environment-Specific Files Use different files for different environments:
.env.development
- Development settings.env.test
- Test environment settings.env.production
- Production defaults (if needed)
#Loading Environment Variables
#Using dotenv
The simplest way to load environment variables is using the dotenv
package:
#Framework-Specific Solutions
Many frameworks have built-in support for environment variables:
#Next.js
- Automatically loads
.env*
files - Supports environment-specific files (
.env.development
,.env.production
) - Next.js Environment Variables Documentation
#Vite
- Automatically loads
.env
files - Supports mode-specific files (
.env.development
,.env.production
) - Vite Env Variables Documentation
#Express
#Nest.js
#Runtime Arguments
You can also supply variables when running your application:
#Production Deployment
#Cloud Platforms
#AWS
- Use AWS Systems Manager Parameter Store for non-sensitive values
- Use AWS Secrets Manager for sensitive values
- Set environment variables in ECS Task Definitions or Lambda configurations
#Google Cloud Platform
- Use Cloud Secret Manager for sensitive values
- Set environment variables in Cloud Run or App Engine configurations
#Azure
- Use Azure Key Vault for sensitive values
- Configure App Settings in Azure App Service
#Container Environments
#Docker
#Kubernetes
#Traditional Hosting
- Set environment variables through the hosting platform's dashboard
- Use deployment scripts to configure environment variables
- Consider using configuration management tools
#Security Best Practices
-
Never commit sensitive values
- Keep
.env
files out of version control - Use secrets management services in production
- Keep
-
Use different values per environment
- Don't share sensitive credentials between environments
- Use environment-specific configurations
-
Access Control
- Limit access to production environment variables
- Use role-based access control for secrets management
-
Encryption
- Encrypt sensitive values at rest
- Use secure channels for transmitting secrets
#Validation and Typesafety
ark.env helps ensure your environment variables are valid:
#Common Patterns
#Configuration Factory
Create a configuration factory to handle different environments:
#Feature Flags
Use environment variables for feature flags:
#Troubleshooting
#Common Issues
-
Missing Variables
- Check if
.env
file exists - Verify variable names match exactly
- Ensure variables are loaded before use
- Check if
-
Type Errors
- Verify variable types match schema
- Check for typos in variable names
- Ensure all required variables are provided
-
Loading Order
- Load environment variables before importing config
- Consider using a bootstrap file
- Check framework-specific loading behavior