Repository navigation
Add a feature to parse .env file by default #51413
Description
Activity
- addedfeature requestIssues requesting new Node.js features.Issues requesting new Node.js features.
on Jan 9, 2024 I propose to parse
.envfile 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
npmornpxcommands do not support--env-fileflag 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 callrequire('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=.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_OPTIONSenv variableNODE_OPTIONS='--env-file=.env' npx myclitool). EDIT: that was wrong, sorry, I forgot--env-fileis not allowed inNODE_OPTIONS/cc @anonrig
Reacted by Tobias NießenReacted by Alexandr LegchilovPlease 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.I'll open a PR for adding a function similar to dotenv.config()
Reacted by Alexandr Legchilov, CogitoBurrito, Mathieu Laurent and Julian CataldoNote that you can also use the
NODE_OPTIONSenv variable (NODE_OPTIONS='--env-file=.env' npx myclitool)/cc @anonrig
Does not work, an error occurs:
node: --env-file= is not allowed in NODE_OPTIONSReacted by Julian Cataldo and Graham Ballantyne- addeddotenvIssues and PRs related to .env file parsing.Issues and PRs related to .env file parsing.
on Jan 18, 2024 Note that you can also use the
NODE_OPTIONSenv variable (NODE_OPTIONS='--env-file=.env' npx myclitool)
/cc @anonrigDoes 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:--env-fileoption in npm-run-script [ENHANCEMENT] Implement--env-fileoption in npm-run-script npm/cli#7069
It would be amazing if it worked with npx
Reacted by Jens Oliver Meiert, moonrockfamily, Justin Mecham, Pixeliner, Artur Kurowski, S. Mahdi Mir-Ismaili, Peter Jaško, Cristian Torres and H. Z. SababaFor 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 .envReacted by Michał WadasReacted by S. Mahdi Mir-Ismaili, xiatian, welder-silva-questores, Graham Ballantyne, James Oates, mikkelal, Cristian Torres and Pier Bover
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
nodekeyword to explicitly specify .env file path. Whereasnpmornpxcommands do not support--env-fileflag 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
.envfile if it exests in project root folder.What alternatives have you considered?
Switching back to dotenv