How do you manage different environment variable values for development, staging, and production environments?
Short answer
You create separate files such as .env.development, .env.production, and rely on your hosting platform's environment variable settings for staging or production secrets, since Next.js automatically loads the appropriate file based on the current environment, letting each…
Advertisement
AModel answer
You create separate files such as .env.development, .env.production, and rely on your hosting platform's environment variable settings for staging or production secrets, since Next.js automatically loads the appropriate file based on the current environment, letting each environment use its own database URLs and API keys.
# .env.development
DATABASE_URL=postgresql://localhost:5432/dev_db
# .env.production
DATABASE_URL=postgresql://prod-server:5432/prod_dbReal-world example
A team uses a local development database while developing, but automatically switches to their production database credentials when the app is deployed live, without ever changing a single line of code.
Likely follow-ups
- How does Vercel handle environment variables for different deployment environments?
- What happens if a required environment variable is missing in production?
Related topics