You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add a feature to parse .env file by default #51413
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.
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".
@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.
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
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.
Reacted by Jens Oliver Meiert, moonrockfamily, Justin Mecham, Pixeliner, Artur Kurowski, S. Mahdi Mir-Ismaili, Peter Jaško, Cristian Torres and H. Z. Sababa
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
Reacted by Michał Wadas
Reacted by S. Mahdi Mir-Ismaili, xiatian, welder-silva-questores, Graham Ballantyne, James Oates, mikkelal, Cristian Torres and Pier Bover
Activity
aduh95 commentedon Jan 9, 2024
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.
Can you give a specific example where this is a problem, ideally in the form "expected behavior vs actual behavior".
ALegchilov commentedon Jan 9, 2024
@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 waynpx myclitool.In current Node there is no way to do it from the code, the only way is cli
--env-fileargument. To set a variables using NodeJs I have to modify my script like thatnode node_modules/@dependency/cli/bin/myclitool.js --env-file=.envTo sum up: it would be enough to have Programmatic API as requested in feature implementation comment. Probably the issue should be renamed respectively.
aduh95 commentedon Jan 9, 2024
Note that you can also use the(NODE_OPTIONSenv variableNODE_OPTIONS='--env-file=.env' npx myclitool). EDIT: that was wrong, sorry, I forgot--env-fileis not allowed inNODE_OPTIONS/cc @anonrig
meyfa commentedon Jan 9, 2024
Please do not do this. We have projects that make use of an
.envfile 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.envwhich 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 commentedon Jan 10, 2024
I'll open a PR for adding a function similar to dotenv.config()
ALegchilov commentedon Jan 10, 2024
Does not work, an error occurs:
node: --env-file= is not allowed in NODE_OPTIONS
penx commentedon Mar 12, 2024
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:--env-fileoption in npm-run-script npm/cli#7069lucasdonato commentedon Mar 19, 2024
It would be amazing if it worked with npx
servernoj commentedon Mar 10, 2025
For those looking for a solution to use
.envwithnpx, here is what I found:npx env-cmd -f .env <my_package>. It will ask you to installenv-cmdbut then will use it transparently to read.envfile and set variables from it without explicit--env-file .env