question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using `vc dev` .env has higher priority than .env.local

See original GitHub issue

Bug report

Describe the bug

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

To Reproduce

https://github.com/gabrielalmeida/nextjs-env-error-issue-17338

Expected behavior

.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)

System information

  • OS: macOS
  • Version of Next.js: 9.5.3
  • Version of Node.js: v12.16.3

Additional context

The same unexpected behavior is present when using now dev or publishing to Vercel.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lfadescommented, Sep 25, 2020

@gabrielalmeida We can have it open here, this is something we would most likely fix with: https://github.com/vercel/next.js/pull/17152 - And tracking this issue is important. Thank you!.

0reactions
rtrembeckycommented, May 24, 2021

hi, what’s the status here? I’m experiencing this problem on 10.2.0. tried to upgrade to 10.2.2 and still a problem. my issue: .env overriding .env.local. my current workaround: use .env.development instead of .env. then .env.local overrides .env.development

Read more comments on GitHub >

github_iconTop Results From Across the Web

What is the difference between .env.local and ... - Stack Overflow
Here's the priority of the files for the development build and the production build: Dev.: ( npm start ): .env.development.local ...
Read more >
Env Variables and Modes - Vite
An env file for a specific mode (e.g. .env.production ) will take higher priority than a generic one (e.g. .env ). In addition,...
Read more >
Compose file version 2 reference - Docker Documentation
Environment variables with only a key are resolved to their values on the machine Compose is running on, which can be helpful for...
Read more >
Modes and Environment Variables - Vue CLI
Mode is an important concept in Vue CLI projects. ... (e.g. .env.production ) will take higher priority than a generic one (e.g. .env...
Read more >
Trying to understand the usages of "env.local" and ... - Reddit
Production environment variables are obviously used for production deployments. The development environment variables are used for local ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found