Skip to content

Improve the way .env configuration file is loaded #49514

Closed as not planned
Closed as not planned

Description

@xavierfoucrier

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

Activity

  1. added
    feature request
    on Sep 6, 2023
  2. anonrig commented on Sep 6, 2023

    @anonrig
    Member

    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.

  3. xavierfoucrier commented on Sep 6, 2023

    @xavierfoucrier
    Author

    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! 🌤️ 🌿

  4. added
    cli
    on Sep 7, 2023
  5. meduzen commented on Oct 15, 2023

    @meduzen

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    clifeature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions