Environment variable loading order contradicts documented behavior
The issue reports that .env files are loading in reverse order of documented precedence, where .env.local should override .env but doesn't. This appears to be a bug in the environment variable loading implementation that needs investigation and potential fixes while considering backwards compatibility.
Doc states the following:
In general only one .env.local file is needed. However, sometimes you might want to add some defaults for the development (next dev) or production (next start) environment.
Next.js allows you to set defaults in .env (all environments), .env.development (development environment), and .env.production (production environment).
.env.local always overrides the defaults set.
Creating .env and .env.local setting the same TEST env variable and console.log shows that .env is actually overriding .env.local
https://github.com/gabrielalmeida/nextjs-env-error-issue-17338
.env.local values should be printed instead of .env "default" values
When running now dev, it actually does print the following wrong loading order for .env* files:
❯ now dev
Now CLI 20.0.0 dev (beta) — https://vercel.com/feedback
info - Loaded env from /Users/gbr/Dev/env-override-error-repro/.env.local
info - Loaded env from /Users/gbr/Dev/env-override-error-repro/.env
ready - started server on http://localhost:3000
event - compiled successfully
event - build page: /
wait - compiling...
event - compiled successfully
.env .env. >>>>>>>>>>>>>> outputs .env instead of .env.local when console.log(process.env.NEXT_PUBLIC_TEST and process.env.TEST)
The same unexpected behavior is present when using now dev or publishing to Vercel.
Claim this issue to let others know you're working on it. You'll earn 20 points when you complete it!