Next.js: Local environment variables do not overwriting stage environment variables

Created on 17 May 2020  路  4Comments  路  Source: vercel/next.js

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
bug needs investigation

Most helpful comment

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

All 4 comments

We'll take a look!

I upgraded from 9.4.2 to 9.4.4, the issue still persists.

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

formula349 picture formula349  路  3Comments

havefive picture havefive  路  3Comments

jesselee34 picture jesselee34  路  3Comments

sospedra picture sospedra  路  3Comments

flybayer picture flybayer  路  3Comments