-
Notifications
You must be signed in to change notification settings - Fork 29.8k
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
src: make --env-file
return warning when not found
#53177
Conversation
Review requested:
|
dotenv
return warning when not found--env-file
return warning when not found
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think #50588 got this right. Node should error if the user tells it to load a file that doesn’t exist. That’s what we do for --require
, --import
and the the other commonly used flags that tell Node to load a file. I understand that this may differ from dotenv
‘s behavior, but we need our CLI to be internally consistent with itself, not with other tools. Generally we error when a specified file cannot be found.
I am also not convinced that “I need to run the same command to start Node for development and for production” is a good idea, much less a use case that we want to support at the expense of some users not getting useful errors, for example if a production server expects an .env
file that is missing but the service starts up anyway. Users who really want to run the same command for development and production can handle this in many ways outside of Node’s scope, such as running the equivalent of touch .env
before starting Node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am +1 on this change if it is semver-major.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This requires documentation.
@MoLow The feature is under |
@targos Can you review again? |
I don’t feel strongly enough about this to block it, but I want @nodejs/tsc to review so that we get a broader opinion of what we think the desired UX should be: either this PR’s warning or the current behavior’s error or the opt-in |
Sounds good to me 👍 |
I wont block but I'm -1 on this change. |
While I agree that errors should be preferable, I think establishing a pattern is also important.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm +1 on this change, having used this option a couple of times for internal scripting, all I really want is an optional way to opt-in to using a .env file while still allowing the same script to be run if someone else in the team wants to run the same script and no file is available in their machine.
I want it to be optional so that it can still be codified into package.json scripts or bash aliases.
What if they all threw a warning, and a new flag, like --throw-on-file-not-found (or similar) would cause them to instead throw. Same for this flag |
I'm fine with landing it, we should probably discourage production use, and explicitly mention that is a development tool and using in production might cause side effect if you forget to configure it properly |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The TSC discussed this in the TSC meeting today and there were no blockers so this can land. |
I don't know what the main use case for If folks use this flag in production, then I strongly believe that ignoring user errors by default is a mistake. Also, the behavior of this flag was changed specifically because users requested the current behavior (i.e., an error), and both polls mentioned above resulted in the majority of respondents saying that they'd expect an error. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer having that configurable (i.e. having a flag to override the default behavior). Once we have that other flag, this PR can then propose flipping the default of this other flag if it has consensus. I'm happy to help making that happen (related: #53060 (comment)).
If we’re going to have two flags, I think it’s much more useful to have a flag for each behavior. Then you can have a required node --env-file=base.env --env-file-optional=local.env app.js |
@tniessen dotenv is mostly used on development not on production. env-file is also aimed to be used for development environment. |
And what size would that be? #53177 (comment) has 27 votes, and |
Hey there, author of #53060 here. I wasn't subscribed to this thread, so I hadn't seen any of this until now, sorry. Seeing as the TSC didn't reach any consensus, and the outcome was "discuss it async in the PR", I'm adding my 2 cents. Honestly, it seems clear to me the majority of users expect an error to be thrown - the 2 polls ran showed >60% of voters picking it. I personally agree with @GeoffreyBooth and prefer that option. If I'm not mistaken, proposed solutions boil down to This throwing an error if file doesn't exist (current behaviour):
This showing a warning if file doesn't exist but executing anyways (this PR):
Adding a new flag
Adding a new flag to this PR that can toggle "strict mode" (not implemented):
Obviously I favour the 3rd approach, as it is the least disruptive (not that it matters at this point, feature is experimental after all) and allows for the highest freedom with the least setup (no duplication of scripts in Maybe we could add a warning if the file doesn't exist and was marked as optional as @jasnell proposed, appeasing that crowd too? It doesn't atm. |
Also, my PR was approved by @mcollina but is blocked by @anonrig. Not sure up to what degree there is a "conflict of interest" here. I do agree Node collaborators' input should be regarded more highly than the general public, but in this case one could argue my PR is also ready to land, so I'm not sure if that's a strong argument in favour of this one in this regard; we're kind of at a stalemate. On a side note, I'm surprised at the amount of discussion such a small change sparked and I'm happy to see such a thriving community chime in and do their best to improve such a widely used tool! |
@anonrig would you be willing to drop your block on #53060? It seems that the majority of users would prefer the error behavior, but we have a substantial minority that want the warning behavior, so I think the reasonable thing to do would be to land #53060 to satisfy both constituencies. |
Here's my personal thoughts on this: Most people have @GeoffreyBooth I still don't think we should maintain 2 separate CLI flags for a development specific functionality. When we first merged If you still insist, I'd like @nodejs/tsc to take a vote on this. |
My personal preference is the status quo so I'm fine with neither PR landing; but I won't block. I think we should respect our user feedback. If we build this feature at all, it should do what users want. |
@anonrig I totally see your point here, but anecdotal evidence is a tough sell:
For example I've never seen such a setup, rather most code I've seen made use of
No need - they can just use the optional flag and keep everything else as is:
These are 2 very fair points I'll concede to. My implementation is objectively more complex, and having a
I agree, but as @GeoffreyBooth said, I'd like to also somehow take users' desires into account. We're never going to get 100% of Node users to vote on such a small issue (or any for that matter), but we should take action somehow. We either
|
For what it’s worth, this is possible already: import { readFile } from 'node:fs/promises'
import { parseEnv } from 'node:util'
try {
const envFileContents = await readFile('.env', 'utf8')
parseEnv(envFileContents)
} catch {} The one thing this doesn’t do is affect Node itself if there’s a |
Seeing as my PR has received two -1s at this stage, I'm personally ok with merging this one in favour of at least improving the current situation, even if it goes against the polls. We can always revisit later with more feedback |
FWIW, we have revisited the feature, which originally only produced a warning. Then, based on more feedback, it was changed to the current behavior. Recent polls, albeit with a small sample size, also indicated that this was preferred by (some subset of) the community. So I am not convinced that merging this PR would actually improve on the status quo. |
@tniessen Fair point. Hoewver, it does force users to either have duplicated scripts (one with, the other without |
@tniessen Why can't we support both options? Throwing a non-negligible subset of the community under the bus would be a fairly questionable approach. This doesn't seem to be the kind of a feature that benefits from being opinionated, especially having in mind that, as @anonrig has pointed out, .env is very frequently used in non-prod environments. |
@kibertoad I don't think I've said anything against supporting both options. (Or perhaps I did a long time ago and don't remember.) |
I'd like to add a new dimension to the discussion from a frontend/full-stack perspective. While I agree with @anonrig's argument that .env files are not used in production environments, this is generally prevalent in the backend world of Node.js. As you might know, on the frontend side, popular tools like Vite and Next.js process environment files with slightly different dynamics. In such codebases, helper scripts or code pieces may need to leverage the References:
Therefore, in a use-case where small scripts are utilized to trigger Prisma/ORM migrations (especially the rise of server-side rendering made full-stack codebases quite popular), we might need to use commands like I may have expanded the scope a lot... but I thought it should be considered when making a decision. |
Closing given the recent TSC vote: https://github.com/nodejs/TSC/blob/main/votes/2024-06-25-0.json |
Currently
node --env-file
throws an error. People tend to add these to their package.json and push it togit
. With this proposed change, we are more in par with existingdotenv
package (for not throwing an error).With the proposed change, we are now warning the users that the
env
file is not found when running the CLI only.loadEnvFile()
is not changed.Alternative was to add a new flag called
--env-file-optional
which I'm 100% against. Ref: #53060Reverts #50588
Closes #50993
Closes #51451