Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement override functionality in setupDotEnv and loadDotEnv functions #190

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

abhi12299
Copy link

This is an issue related to a draft PR I raised in nuxt/cli#466

Issue Description

  • setupDotEnv function is called with a .env file. Assume that the only env var loaded is FLAG.
  • The FLAG variable's value is updated in the .env file by the running process.
  • setupDotEnv is called again, but the value of process.env.FLAG is not changed.

Reproduce the issue: https://stackblitz.com/edit/stackblitz-starters-xnps4y?file=.env,index.js

I have introduced an override flag in DotenvOptions, which gives precedence to the env variable values in the .env file over the existing values in process.env.

With this change, the PR nuxt/cli#466 can be simplified and we can use this new flag directly.

src/dotenv.ts Outdated Show resolved Hide resolved
@northword
Copy link

I was looking for an option for this problem, thanks for your work!

I think this option makes sense because in some cases, a person may be maintaining multiple projects of the same type, so some of the environment variables are consistent, so the developer can store these environment variables to the system/user level environment variables instead of configuring them separately for each project. This option is useful when a project needs to override system/user level environment variables.

The dotenv package itself supports this option, but I read the source code for both dotenv and c12, and found that using dotenv's code directly is not feasible because c12 supports custom filenames and adds extra handling for keys starting with _.

Hopefully c12 will consider merging this pr.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants