🚀 Feature: if loading a file as CJS and ESM both fail, display both errors instead of just ESM #5235
Open
3 tasks done
Labels
status: waiting for author
waiting on response from OP - more information needed
type: feature
enhancement proposal
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: