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

Improve the way .env configuration file is loaded #49514

Closed
xavierfoucrier opened this issue Sep 6, 2023 · 3 comments
Closed

Improve the way .env configuration file is loaded #49514

xavierfoucrier opened this issue Sep 6, 2023 · 3 comments
Labels
cli Issues and PRs related to the Node.js command line interface. feature request Issues that request new features to be added to Node.js.

Comments

@xavierfoucrier
Copy link

What is the problem this feature will solve?

Make environment variables configuration file loading more fluent.

What is the feature you are proposing to solve the problem?

Instead of loading it using a CLI command like node --env-file .env, it would be event better to use a package.json field containing the environment filename, this way, Node can automatically load the environment variables every time a NPM script is running.

Another idea would be to let Node detect if a default .env file exists, and load it for each NPM script that is running. According to this idea, may be we could have a flag like --no-env-file or --env-file none to tell Node to not load environment file by default.

This could be the default behavior.

What alternatives have you considered?

No response

@xavierfoucrier xavierfoucrier added the feature request Issues that request new features to be added to Node.js. label Sep 6, 2023
@github-project-automation github-project-automation bot moved this to Pending Triage in Node.js feature requests Sep 6, 2023
@anonrig
Copy link
Member

anonrig commented Sep 6, 2023

Another idea would be to let Node detect if a default .env file exists, and load it for each NPM script that is running.

This would be a breaking change, and by default opt-in as well as automatically detecting might create some serious security issues.

Instead of loading it using a CLI command like node --env-file .env, it would be event better to use a package.json field containing the environment filename, this way, Node can automatically load the environment variables every time a NPM script is running.

package.json fields are defined under WinterCG that would be hard to convince and requires a JSON parser that would be harder to implement since V8 is not initialized when parsing occurs.

@xavierfoucrier
Copy link
Author

xavierfoucrier commented Sep 6, 2023

Hi @anonrig 👋

Thanks for the fast reply, that was just some suggestions 😉, after doing a few tests on my side.
Unfortunately, from my point of view, I am not seeing all use cases / breaking changes / security issues that this may imply.

Feel free to close the issue if you think it's out of scope.

Have a nice day! 🌤️ 🌿

@anonrig anonrig closed this as not planned Won't fix, can't repro, duplicate, stale Sep 7, 2023
@anonrig anonrig added the cli Issues and PRs related to the Node.js command line interface. label Sep 7, 2023
@meduzen
Copy link

meduzen commented Oct 15, 2023

Jumping here, as I was searching for something similar.

For npm scripts specifically isn’t it a problem space that should be tackled by package managers like npm? 🤔

What led me here:

I wanted to avoid using dotenv in a Vite config file, which is possible but ends up changing NPM scripts from:

"build": "vite build --mode=production",

to

"build": "node --env-file=.env node_modules/vite/bin/vite vite build --mode=production",

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Issues and PRs related to the Node.js command line interface. feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

No branches or pull requests

3 participants