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.

Local environment variables do not overwriting stage environment variables

See original GitHub issue

Bug report

Describe the bug

Environment variables are loaded in the following order:

ready - started server on http://localhost:3000
info  - Loaded env from .env.development.local
info  - Loaded env from .env.development
info  - Loaded env from .env.local
info  - Loaded env from .env

However, vars in .env.local do not overwrite vars in .env.development. Is this to be expected?

The use case arises when a var in development should be overwritten when running locally. Say accessing the api.

// .env.development
NEXT_PUBLIC_API_URL="https://api.example.com"
// .env.local
NEXT_PUBLIC_API_URL="http://localhost:3000"

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  1. Create a .env.development file and add a variable.
  2. Create a .env.local file and add the same variable.
  3. console log the variable in a page
  4. See the development var and not the local one.

Expected behavior

Local variables should overwrite previous variable set in both .[stage] and .[stage].local env files.

System information

  • OS: macOS
  • Version of Next.js: 9.4.0 & 9.4.1
  • Version of Node.js: 10.16.3

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
hect1ccommented, Jul 13, 2020

Hey,

Maybe I misunderstand but with 9.4.4 when using

  • next start or next build - environment will be production
  • next dev - environment will be development

In order to override these environments I can pass in NODE_ENV=development npm run build and this should set the environment to development?

My issue is even in doing this next will still load the .env. and .env.production files, should it not load the .env.development file instead? What am I missing to achieve this as I would like to load the correct files based on custom environments I set. Thanks

0reactions
balazsorban44commented, Jan 29, 2022

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Local environment variables do not overwriting stage ... - GitHub
Expected behavior. Local variables should overwrite previous variable set in both .[stage] and .[stage].local env files.
Read more >
Dotenv unable to overwrite key value pair - Stack Overflow
Environment variables that are already set will not be overwritten, that means that the command line variables have a higher priority over ...
Read more >
Overwriting environment variables when invoking local function
Hey,. I am setting environment variables in serverless.yml like this: environment: DYNAMODB_TABLE: Ref: DynamodDBTable. That does not work ...
Read more >
Is it possible to declare a local variable without overriding the ...
A workaround is to save and restore the value. That does mean listing the variables you want to save. You can indeed clash...
Read more >
Working with Environment Variables in Node.js - Twilio
Environment variables are a great way to configure parts of your Node.js application. Learn how to work with them using helpful tools such ......
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