kerimdzhanov / dotenv-flow

Loads environment variables from .env.[development|test|production][.local] files for Node.js® projects.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

variables overwriting/priority does not match Next.js

timhettler opened this issue · comments

The file priority for this project does not match the convention in Next.js

  1. process.env
  2. .env.$(NODE_ENV).local
  3. .env.local (Not checked when NODE_ENV is test.)
  4. .env.$(NODE_ENV)
  5. .env

(See: https://nextjs.org/docs/pages/building-your-application/configuring/environment-variables#environment-variable-load-order)

It would be great to have an option to match this convention.

Hi @timhettler,

Thanks for the suggestion.

Interesting… So, as far as I understand, the difference is that .env.local has priority over .env.${NODE_ENV}. It definitely makes some kind of sense.

I'm thinking about the name and variations of the option and considering "order" or "priority" cannot imagine anything obvious/easy to read. The only readable solution I could imagine so far is providing a list of files in the order you'd like them to be loaded. It would look something like this:

require('dotenv-flow').config({
  files: [
    '.env',
    `.env.${process.env.NODE_ENV}`,
    '.env.local',
    `.env.${process.env.NODE_ENV}.local`,
  ]
});

(shell/predefined variables will always have a priority over those defined in env files)

Any other ideas?

This solution definitely gives the user the most control! Alternatively, as a convenience, you could provide something like sortingStyle?: 'nextjs' | 'default' config option. I'm not aware if there are other frameworks that use a different convention.

Heya @timhettler 🙌

Here is a PR: #87. Please take a look when you have a free moment. If everything is good I'll release it as v4.1.0.

Thank you for this change!