Open jedwards1211 opened 5 days ago
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?
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