Which file wins? Load order in real tools
| Tool | Files | Who wins |
|---|---|---|
| Next.js | .env, .env.local, .env.[mode], .env.[mode].local | .env.[mode].local > .env.local > .env.[mode] > .env; real environment variables beat all |
| Vite | same names | mode-specific beats generic, .local beats non-local; existing env vars beat all |
node --env-file=a --env-file=b | any | the later file (b) |
dotenv config({ path: [a, b] }) | any | the first file (a), unless override: true |
Docker Compose env_file: [a, b] | any | the later file (b); environment: beats both |
To reproduce a framework in the merger, add the layers from lowest to highest priority. For Next.js production that is .env, .env.production, .env.local, .env.production.local.
Merging on the command line
# later files win: keep the LAST definition of each key cat .env .env.production .env.production.local \ | grep -E '^(export )?[A-Za-z_][A-Za-z0-9_]*=' \ | tac | awk -F= '!seen[$1]++' | tac > .env.merged
This keeps the last definition per key in its original position (tac is tail -r on macOS). After merging, the ENV cleaner removes leftovers and the .env formatter sorts and groups the result.
Checking the merge result
- Look at the source list under the output: every key shows its winning file, and yellow notes show what it overrode.
- Compare the merged file with what production actually has, using the ENV diff checker.
- Check that nothing required is missing with the missing env detector against your
.env.example.