.env.local.production -
.env.local.production becomes the gatekeeper for those hyper-specific, non-shareable configs. Before you rush to create .env.local.production , understand the risks. This file sits in a difficult position between convenience and catastrophe. 1. The Gitignore Nightmare Because .env.local.production is "local," it should always be in .gitignore . But developers often copy-paste ignore rules without verifying.
// Order of precedence (lowest to highest priority) const files = [ .env , .env.$nodeEnv , .env.local , .env.$nodeEnv.local , .env.local.$nodeEnv // Support for the inverted pattern ]; .env.local.production
# .env.local.production (not in Git) DATABASE_URL="postgresql://localhost:5432/prod_mirror" STRIPE_SECRET_KEY="sk_test_localDebugKey" NEXT_PUBLIC_ANALYTICS_ID="debug-123" This file allows you to simulate a production environment without touching real production secrets. Sometimes, the process of building your application (minification, bundling, tree-shaking) requires specific flags. For example, you might enable source maps only in local production builds, but not in real production. // Order of precedence (lowest to highest priority)
When you run npm run build --mode=production , the system loads .env.production , then overwrites it with .env.local.production . If your application must work in an offline environment (e.g., an IoT device, a ship, or a secure government facility), you might prepopulate caches, mock external APIs, or use local fallbacks. These settings should only be active when NODE_ENV=production and you are on a specific approved machine. override: true )
for (const file of files) const result = dotenv.config( path: path.resolve(process.cwd(), file), override: true ); if (result.error && result.error.code !== 'ENOENT') console.warn( Error loading $file: , result.error);