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

Question: Possible to load multiple .env files into the shell? #36

Open
basickarl opened this issue Mar 5, 2020 · 7 comments · May be fixed by #50
Open

Question: Possible to load multiple .env files into the shell? #36

basickarl opened this issue Mar 5, 2020 · 7 comments · May be fixed by #50

Comments

@basickarl
Copy link

basickarl commented Mar 5, 2020

Question: Is it possible to load multiple .env files into the shell?

npx dotenv-extended --path=first.env dotenv-extended --path=second.env node herro.js

Or something like:

npx dotenv-extended --path=first.env,second.env node herro.js

@keithmorris
Copy link
Owner

@basickarl can you give me a use case for this? This would require a bit if refactoring to support and I'm not sure how often this would be used.

@dmitryvmin
Copy link

dmitryvmin commented Apr 13, 2020

One use case would be if your configs are env specific - "qa", "prod", etc.

Something like would be useful:

env.qa.defaults + .env.qa
env.prod.defaults + env.prod

@keithmorris
Copy link
Owner

@dmitryvmin It seems that this can already be accomplished with the dotenv-extended command:

e.g. for QA:
dotenv-extended --path=/path/to/.env.qa --defaults=/path/to/.env.qa.defaults --errorOnMissing=true ./myshellscript.sh --whatever-flags-my-script-takes

Then for prod:
dotenv-extended --path=/path/to/.env.prod --defaults=/path/to/.env.prod.defaults --errorOnMissing=true ./myshellscript.sh --whatever-flags-my-script-takes

Am I misunderstanding?

@mvastola
Copy link

I was about to post this same feature request. The idea would be to allow for multiple layers of defaults/settings, as provided by dotenv-flow (which is fairly popular) as well as similar modules.

These other modules, however, aren't easily made compatible with dotenv-extended, however, without allowing the overriding of values already in the environment. (Or otherwise by saving the original environment and re-applying it after the changes in this module.)

Implementing this feature a really simple change as well. The vast majority of the change is simply updating src/index.js to say

let envFiles = [];
[ options.defaults, options.path ].forEach(files => {
  if (!files || files.length === 0) return;
  if (!Array.isArray(files)) files = [ files ];
  envFiles.push(...files);
});
let configData = files.reduce((accumulator, file) => {
  let fileData = loadEnvironmentFile(file, options.encoding, options.silent);
  return { ...accumulator, ...fileData };
});

Beyond that, pretty much only CLI argument parsing would need to be tweaked, and documentation would need to be updated.

This was referenced Apr 23, 2021
@goatandsheep
Copy link

@mvastola so like this? #50

@basickarl
Copy link
Author

@goatandsheep You perhaps need to update the tests?

@mvastola
Copy link

@goatandsheep , it looks like you copied my code verbatim (and my code still makes sense to me, surprisingly), so I'm going to guess 'yes', but I honestly barely remember this request, hah.

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 a pull request may close this issue.

5 participants