Skip to content

Add a feature to parse .env file by default #51413

Description

@ALegchilov

What is the problem this feature will solve?

As of now current NodeJS requres to explicitly specify path to .env file whereas dotenv makes it by default. Users have to run scripts starting with node keyword to explicitly specify .env file path. Whereas npm or npx commands do not support --env-file flag to pass it to node. For users who wants to exclude useage of external dotenv it seems inconvenient.

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

In order to get rid of dotenv library I propose to parse .env file if it exests in project root folder.

What alternatives have you considered?

Switching back to dotenv

Activity

  1. aduh95 commented on Jan 9, 2024

    @aduh95
    Contributor

    I propose to parse .env file if it exests in project root folder.

    This would of course introduce a security issue bad enough that we will probably never have this behavior as default. However there might be other ways to solve the problem at hand.

    Whereas npm or npx commands do not support --env-file flag to pass it to node.

    Can you give a specific example where this is a problem, ideally in the form "expected behavior vs actual behavior".

  2. ALegchilov commented on Jan 9, 2024

    @ALegchilov
    Author

    @aduh95
    With dotenv I just call require('dotenv').config() to programmatically set environment variables from a .env file. I run a cli tool in such a way npx myclitool.
    In current Node there is no way to do it from the code, the only way is cli --env-file argument. To set a variables using NodeJs I have to modify my script like that node node_modules/@dependency/cli/bin/myclitool.js --env-file=.env
    To sum up: it would be enough to have Programmatic API as requested in feature implementation comment. Probably the issue should be renamed respectively.

  3. aduh95 commented on Jan 9, 2024

    @aduh95
    Contributor

    Note that you can also use the NODE_OPTIONS env variable (NODE_OPTIONS='--env-file=.env' npx myclitool). EDIT: that was wrong, sorry, I forgot --env-file is not allowed in NODE_OPTIONS

    /cc @anonrig

  4. meyfa commented on Jan 9, 2024

    @meyfa
    Contributor

    Please do not do this. We have projects that make use of an .env file for consumption by another application, not directly related to Node.js, but those projects use Node.js tooling. If Node.js started automatically parsing the .env which is not meant for it, suddenly it would be using variables that it previously didn't, which would break the current behavior in a few cases.

  5. anonrig commented on Jan 10, 2024

    @anonrig
    Member

    I'll open a PR for adding a function similar to dotenv.config()

  6. self-assigned this
    on Jan 10, 2024
  7. moved this from Pending Triage to In Progress in Node.js feature requestson Jan 10, 2024
  8. ALegchilov commented on Jan 10, 2024

    @ALegchilov
    Author

    Note that you can also use the NODE_OPTIONS env variable (NODE_OPTIONS='--env-file=.env' npx myclitool)

    /cc @anonrig

    Does not work, an error occurs:
    node: --env-file= is not allowed in NODE_OPTIONS

  9. added
    dotenvIssues and PRs related to .env file parsing.
    on Jan 18, 2024
  10. penx commented on Mar 12, 2024

    @penx

    Note that you can also use the NODE_OPTIONS env variable (NODE_OPTIONS='--env-file=.env' npx myclitool)
    /cc @anonrig

    Does not work, an error occurs: node: --env-file= is not allowed in NODE_OPTIONS

    Was there a solution loading environment variables from .env when running npx? I don't see a solution in the linked PR.

    There is a related issue against npm/cli but it was closed:

  11. lucasdonato commented on Mar 19, 2024

    @lucasdonato

    It would be amazing if it worked with npx

  12. moved this from In Progress to Done in Node.js feature requestson Jun 29, 2024
  13. servernoj commented on Mar 10, 2025

    @servernoj

    For those looking for a solution to use .env with npx, here is what I found: npx env-cmd -f .env <my_package>. It will ask you to install env-cmd but then will use it transparently to read .env file and set variables from it without explicit --env-file .env

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

dotenvIssues and PRs related to .env file parsing.feature requestIssues requesting new Node.js features.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions