-
-
Notifications
You must be signed in to change notification settings - Fork 3k
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
🚀 Feature: if loading a file as CJS and ESM both fail, display both errors instead of just ESM #5235
Comments
This is tricky. If both CJS and ESM fail to import the file because it actually doesn't exist, then displaying both errors would be extra noise for users. If this were to be implemented it would probably need some kind of heuristic for determining whether the errors are different enough to both warrant being displayed. I don't know how straightforward that would be to implement. Would you be able to post a minimum reproduction project we could look at to better understand this? |
Okay I'll work on making a repro soon |
I don't think the extra noise would be worse, because what it currently displays is very confusing at first in a situation like this. Theoretically the heuristic could be whether ERR_REQUIRE_ESM came from the user's own project files or from node_modules |
👋 just checking in @jedwards1211, any luck on a repro? |
Sorry I forgot about that, here you go: https://github.com/jedwards1211/mocha-cjs-esm |
Ok thanks - I got a chance to look at the repro and it makes sense. The heuristic for which error is thrown comes from #4803 -> #4807: The Lines 41 to 80 in 73bb819
I was able to fix this issue locally by having it Accepting PRs, with the caveat that fixing this will require being able to explain why the fix is correct. Chesterton's Fence applies. 🙂 |
This makes sense. |
But maybe a better solution would be to remove the use of But we're in 2024. I would just use |
@giltayar using |
I mean, it would probably end up calling |
That's a really interesting idea @giltayar. If all test cases pass, then that's lovely. In fact, I tried the latest Node 23.5.0, and the test case repro no longer fails!
https://nodejs.org/docs/latest/api/modules.html#loading-ecmascript-modules-using-require: in 22.12.0 and 23.5.0, So, I bet we can actually:
Thoughts? |
@JoshuaKGoldberg as I mentioned above would probably break everyone using typescript... try making testcases that use ts-node/register with CJS... |
Right, what I mean is: mark a followup issue for 2. to see if that's still the case in the future. |
Well it should still be an issue for no. 1 in the newest versions of Node. |
Gotcha. For 1, I'm not following how the difference between JS and TS users will be relevant in the newest versions of Node when using Supporting evidence: I pushed up https://github.com/JoshuaKGoldberg/mocha-examples/tree/use-chai-in-typescript-examples using
Footnotes |
Feature Request Checklist
faq
label, but none matched my issue.Overview
Ever since
chai
v5 dropped support for CJS, we get errors like this when we accidentally install chai 5+:This threw us for a loop at first, because normally our tests work fine with
@babel/register
handling.ts
files.What's happening is first Mocha tries to load our file as CJS, which make it as far as
require('chai')
which fails withERROR_REQUIRE_ESM
. Then Mocha tries to load our file as ESM instead, which triggers the above error.Suggested Solution
If Mocha would print out the error from loading our file as CJS in addition to the above error, we would have seen what caused the problem more quickly.
Alternatives
I've thought about adding code to our toolchain to check for chai v5+ and explicitly warn...but this same issue could occur with any package we're using in tests that drops CJS support
I guess another solution would be a mocharc option to force Mocha to only try one of CJS or ESM, not both.
I can't just put
type: "commonjs"
in mypackage.json
because I made my toolchain run tests in CJS mode with@babel/register
, and then in ESM mode withbabel-register-esm
, since I'm transpiling my TypeScript source to both CJS and ESM for publishing.Additional Info
No response
The text was updated successfully, but these errors were encountered: