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

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

Closed
ALegchilov opened this issue Jan 9, 2024 · 8 comments · Fixed by #51476
Closed

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

ALegchilov opened this issue Jan 9, 2024 · 8 comments · Fixed by #51476
Assignees
Labels
dotenv Issues and PRs related to .env file parsing feature request Issues that request new features to be added to Node.js.

Comments

@ALegchilov
Copy link

ALegchilov commented Jan 9, 2024

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

@ALegchilov ALegchilov added the feature request Issues that request new features to be added to Node.js. label Jan 9, 2024
@github-project-automation github-project-automation bot moved this to Pending Triage in Node.js feature requests Jan 9, 2024
@aduh95
Copy link
Contributor

aduh95 commented Jan 9, 2024

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".

@ALegchilov
Copy link
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.

@aduh95
Copy link
Contributor

aduh95 commented Jan 9, 2024

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

@meyfa
Copy link
Contributor

meyfa commented Jan 9, 2024

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.

@anonrig
Copy link
Member

anonrig commented Jan 10, 2024

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

@anonrig anonrig self-assigned this Jan 10, 2024
@anonrig anonrig moved this from Pending Triage to In Progress in Node.js feature requests Jan 10, 2024
@ALegchilov
Copy link
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

@penx
Copy link

penx commented Mar 12, 2024

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:

@lucasdonato
Copy link

It would be amazing if it worked with npx

@RedYetiDev RedYetiDev moved this from In Progress to Done in Node.js feature requests Jun 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dotenv Issues and PRs related to .env file parsing feature request Issues that request new features to be added to Node.js.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants