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
#50588 introduced a throw if a requested env file isn't found. When dealing with multiple environments, a .env file may sometimes be merely optional - and throwing isn't desired. For example, during local development, devs might keep variables in a .env file, which isn't committed to source control, while in production, these variables come from the environment. This option will allow the env file to be loaded only if it exists without changing the command used to invoke node between environments.
What is the feature you are proposing to solve the problem?
Add a flag (or other mechanism) to indicate that the .env file should be loaded only it's found, and to throw otherwise.
What alternatives have you considered?
touching .env before running node; invoking node via a shell script and "building up" the cli opts.
Another simple (yet non-portable) alternative is to put something like this in your .bashrc or so:
functionenv-file-if-exists() { if [ -f"$1" ];thenecho"--env-file=$1";fi }
And then use it like this:
node $(env-file-if-exists local.env)
Or, for better readability, something like this:
functionif-exists() { if [ -f"$1" ];thenecho"$1";elseecho /dev/null;fi }
To be used as:
node --env-file=$(if-exists local.env)
For example, during local development, devs might keep variables in a .env file, which isn't committed to source control, while in production, these variables come from the environment.
On a side note, from a security point of view, I think this is an anti-pattern. Loading environment variables from a file that may or may not exist opens up a potential attack vector.
I second that it would be really desirable to have this feature. We're working with a .env and a .env.local file in our projects, the latter only being available locally. If this doesn't work anymore, the whole feature is rendered useless for us. You could put all your resentiments against such a feature in it's documentation: "Warning! It's an attack vector, it's not deterministic." etc., and us daredevils could use it despite those warnings. ;)
Reacted by Mehdi, Jonatan Pedersen , Nicholas Kyriakides and Gustavo
Conundrum: use the "native" feature and add complexity to your own packages OR use the pre-existing npm package which is "not native" and requires an additional dependency tree.
I have a similar issue, posted here (and got 💩 on but ok).
The recc. here would help me but Node is meant to be a generic runtime, not a batteries-included server package. insisting on installing zilch and instead asking it to do all the lifting is starting to sound a bit much.
I have the same philosophy and don't install unless abs. necessary.
We're currently trying out this scheme, with 2 files checked in version control:
I think this kind of sucks, it's convoluted and has potential of someone seeing an .env-like file
and pushing his grandmothers banking credentials up in VC but why would I care?
A Github org. can setup server git hooks and reject anything resembling a cred.
I support a --optional-env-file argument. I don't see how this would be more of an attack vector than --env-file is already. This would simply go forth regardless of whether it found the desired .env file, and it would allow not having to do the solutions mentioned in #51451 nor having to implement the scripts above (which don't work in Windows)
Edit: Solved in #53060. Now we have to wait for it to be merged
I was setting up a GitHub action for my project and wanted to use secrets to set env variables in the YAML file depending on if it was a production or development build. In this situation, the .env file for developers is missing for the build server since it's excluded via .gitignore. It looks like the work-around is to create a .env file for each build.
I was setting up a GitHub action for my project and wanted to use secrets to set env variables in the YAML file depending on if it was a production or development build. In this situation, the .env file for developers is missing for the build server since it's excluded via .gitignore. It looks like the work-around is to create a .env file for each build.
That's exactly what we've been doing until now while waiting for a non-mandatory env injection version. All of our env-sensible scripts look something like that: touch .env && node --env-file .env scripts/xyz.js. This keeps the current .env file if one exists, and creates an empty one if it doesn't. Not a huge deal, but useless IMHO compared to an optional version. Let's be patient!
Activity
tniessen commentedon Dec 1, 2023
A few random thoughts:
Another simple (yet non-portable) alternative is to put something like this in your
.bashrcor so:And then use it like this:
node $(env-file-if-exists local.env)Or, for better readability, something like this:
To be used as:
node --env-file=$(if-exists local.env)On a side note, from a security point of view, I think this is an anti-pattern. Loading environment variables from a file that may or may not exist opens up a potential attack vector.
hulkish commentedon Dec 15, 2023
this is easily achievable with a little shell statement:
node $([[ -f .env ]] && echo "--env-file .env ")index.jsmbrevda commentedon Dec 15, 2023
This request is for a pure node solution
JonasDoe commentedon Jan 22, 2024
I second that it would be really desirable to have this feature. We're working with a
.envand a.env.localfile in our projects, the latter only being available locally. If this doesn't work anymore, the whole feature is rendered useless for us. You could put all your resentiments against such a feature in it's documentation: "Warning! It's an attack vector, it's not deterministic." etc., and us daredevils could use it despite those warnings. ;)Update dependencies
iambumblehead commentedon Apr 29, 2024
Conundrum: use the "native" feature and add complexity to your own packages OR use the pre-existing npm package which is "not native" and requires an additional dependency tree.
edit: original comment was maybe rude
nicholaswmin commentedon Apr 30, 2024
I have a similar issue, posted here (and got 💩 on but ok).
The recc. here would help me but Node is meant to be a generic runtime, not a batteries-included server package. insisting on installing zilch and instead asking it to do all the lifting is starting to sound a bit much.
I have the same philosophy and don't install unless abs. necessary.
We're currently trying out this scheme, with 2 files checked in version control:
.env.local.dev.env.local.testwhich list reasonable safe defaults, ie:
All other
.envfile formats are.gitignoredHere's the runners:
internally we do something like this:
I think this kind of sucks, it's convoluted and has potential of someone seeing an
.env-like fileand pushing his grandmothers banking credentials up in VC but why would I care?
A Github org. can setup server git hooks and reject anything resembling a cred.
BoscoDomingo commentedon May 13, 2024
I support a
--optional-env-fileargument. I don't see how this would be more of an attack vector than--env-fileis already. This would simply go forth regardless of whether it found the desired .env file, and it would allow not having to do the solutions mentioned in #51451 nor having to implement the scripts above (which don't work in Windows)Edit: Solved in #53060. Now we have to wait for it to be merged
lewismoten commentedon May 27, 2024
I was setting up a GitHub action for my project and wanted to use secrets to set env variables in the YAML file depending on if it was a production or development build. In this situation, the
.envfile for developers is missing for the build server since it's excluded via.gitignore. It looks like the work-around is to create a.envfile for each build.acidoxee commentedon May 27, 2024
That's exactly what we've been doing until now while waiting for a non-mandatory env injection version. All of our env-sensible scripts look something like that:
touch .env && node --env-file .env scripts/xyz.js. This keeps the current.envfile if one exists, and creates an empty one if it doesn't. Not a huge deal, but useless IMHO compared to an optional version. Let's be patient!avivkeller commentedon Jun 27, 2024
The TSC had a vote on this, and IIRC this is the plan
https://github.com/nodejs/TSC/blob/main/votes/2024-06-25-0.json
src: add `--env-file-if-exists` flag
src: add `--env-file-if-exists` flag
src: add `--env-file-if-exists` flag