-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
module: clarify esm error message in require
calls
#57774
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
base: main
Are you sure you want to change the base?
module: clarify esm error message in require
calls
#57774
Conversation
The current error message that is thrown when an ES module calls `require` simply suggests to "use import instead" this is slightly ambiguous since in JavaScript there are two types of imports: static and dynamic. This change simply clarifies the error message by clearly suggesting to use either a static or dynamic import
Review requested:
|
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #57774 +/- ##
==========================================
- Coverage 90.24% 90.23% -0.02%
==========================================
Files 630 630
Lines 185188 185245 +57
Branches 36275 36303 +28
==========================================
+ Hits 167130 167157 +27
- Misses 11001 11016 +15
- Partials 7057 7072 +15
🚀 New features to boost your workflow:
|
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
As someone who rarely interacts with ESM, the new message is as clear as the old message. |
Hi @jsumners-nr 🙂 , thanks for the comment, I understand that I am not adding much to the error message but as I mentioned in the PR description I was trying not to be too verbose and assume a minimal level of familiarity with the modules system and/or the possibility for developers to google the missing information. Would you have a potential alternative error message in mind that you can suggest that can be clearer without it being too verbose? 🙂 |
Maybe, if I knew what either message was trying to tell me to do. Possibly include a link to documentation that explain the error and shows examples of correcting it? |
(very briefly and simplified) in nodejs there are two types of modules cjs and esm, in the former you can use
That could be a valid idea but by looking at the code this seems like a pattern very seldom used in nodejs (I can only see a handful of such occurrences) 🤔 , I also think that a pattern like that has its own maintainability drawbacks since docs can change over time and any reference to them (AFAICT) needs potential manual efforts to keep in sync 🤔 |
@dario-piotrowicz explaining it in a GH comment is fine, but I think the point of @jsumners-nr was that the error message should be more informative. I agree with that, if someone doesn't know what |
Exactly. As I stated, I deal with ESM as little as possible. Differentiating between "static" and "dynamic" import statements, when I was clearly trying to use the simpler Additionally, it's probably far more common for this error to be encountered because require ESM is no longer behind a flag. So what was once working is now throwing an obscure error that the developer might not understand. In my opinion, if this is going to be improved, the most viable way I can see is by adding a permanent link to docs that outline the problem and show solutions. As for docs moving, there's a standard out there called "HTTP". In it, there are well defined error codes and mechanisms for content that has changed addresses. These are called redirects, and if any linked docs move in the future then they should be accompanied by an appropriate redirect. For example, https://developer.mozilla.org/en-US/docs/Web/HTTP/Reference/Status/301. |
|
Why would such an assumption be reasonable? I am quite serious that I don't know what the proposed change is meant to make me understand. For me, a "dynamic import" looks something like: const foundName = lookUpSomeName()
const importedModule = require(`${foundName}`) Is that what the error message wants me to do? The ESM import syntax is complicated, and has many permutations. I highly doubt I'm the only one who has learned the basic My point is: if the intention is to make the error message CLEAR, then the proposed change is insufficient. |
@jsumners-nr since you know that Things don't have to be clear immediately, as long as they can unambiguously lead to clarity. I've never used or heard the term "dynamic import" to mean the same as "dynamic require", so that one really confuses me. |
I disagree.
"import" means to include source code from somewhere else. It does not necessarily mean the |
Well I decided to go with the change exactly because the issue description in #40544 shows, I think, that the current error message confused the reporter which assumed that the only appropriate solution was a dynamic import. So updating it to clearly say to use one or the other should at least hopefully address that sort of confusion I believe/hope 🙂
I see... so I take it you'd prefer to keep the current wording? 🥲 (PS: from my point of view, I am not removing this "vagueness" feature, but just slightly reducing it to hopefully nudge people a bit more in the right direction) |
Yes I don't think the wording in this PR is decreasing potential confusion, so my preference would be to not land as is (but I don't feel strongly about it). |
I think the improvement is the removal of "you can", making the tone more formal, but yes the message is not clearer. |
I also don't feel too strongly about it landing, although I do think it marginally improves things but it clearly it's a super impactful change 🙂
Mh... I'm not completely sure I agree with that, as I mentioned in the issue I believe the reporter was asking to clarify that the tool to use was a dynamic import and the changes here are clarifying that you can use either a static or dynamic one, so that makes me think that this addressed to some extent the reporter's confusion 🤔
Agreed 👍
Would you like me to go with that solution then? and/or what would your preference be here? By the way, have you seen the PR I linked in my PR description: #53152 |
The current error message that is thrown when an ES module calls
require
simply suggests to "use import instead" this is slightly ambiguous since in JavaScript there are two types of imports: static and dynamic. This change simply clarifies the error message by clearly suggesting to use either a static or dynamic importIf someone is interested in a before-after screenshot:

Fixes #40544
Note
This PR replaces the stalled #53152, that PR provided more help to the user than mine does, I however softly disagree with that approach as I think we can assume a certain level of understanding by developers of how modules work, and if they don't it should be simple enough for them to refer to the docs.
So I am simply clarifying that both static and dynamic imports can be used here (I assume everyone knows what those are, if not, googling/mdning it should be pretty trivial) rather than throwing errors with long elaborate explanatory messages
If someone disagree and thinks that we should indeed provide more details to users I'm happy to try to get closer to the solution in #53152