But as your project scales, you quickly outgrow the simple .env file. You discover the "stacking" system: .env , .env.local , .env.production , .env.testing . Then, you stumble upon a hybrid beast: .
console.log('Loading env from:', process.env.NODE_ENV); console.log('API Key:', process.env.API_KEY); Watch your terminal when you run next build or next start . If your framework uses dotenv : .env.local.production
export default function handler(req, res) res.status(200).json( nodeEnv: process.env.NODE_ENV, customVar: process.env.MY_CUSTOM_VAR, // Warning: Do not do this in real production allEnv: process.env ); But as your project scales, you quickly outgrow the simple
Do not use both. Use .env.local for development mode. Use .env.production.local exclusively for production mode debugging. Part 7: Debugging – Is My File Being Loaded? It is notoriously difficult to know which env file is active. Here is how to check. Method 1: Dump the Config (Next.js) Create a file pages/api/debug.js : console
docker run --env-file ./docker/prod-override.env myapp:latest On platforms like Vercel, you never use .env.production.local . You use their dashboard or CLI: