Repository navigation
🐛 Bug: Mocha 11.7.0 introduces issues loading ECMAScript Modules (ESM) with ts-node/esm #5388
Copy link
Copy link
Closed
Labels
status: in triagea maintainer should (re-)triage (review) this issuea maintainer should (re-)triage (review) this issuetype: buga defect, confirmed by a maintainera defect, confirmed by a maintainer
Description
Bug Report Checklist
- I have read and agree to Mocha's Code of Conduct and Contributing Guidelines
- I have searched for related issues and issues with the
faqlabel, but none matched my issue. - I have 'smoke tested' the code to be tested by running it outside the real test suite to get a better sense of whether the problem is in the code under test, my usage of Mocha, or Mocha itself.
- I want to provide a PR to resolve this
Expected
npm run testto output:
Works fine
√ ESM TypeScript Test
1 passing (2ms)
Actual
npm run testnow results in:
Exception during run: Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\Borewit\IdeaProjects\mocha-esm-issue\index.js' imported from C:\Users\Borewit\IdeaProjects\mocha-esm-issue\test.ts
at finalizeResolution (node:internal/modules/esm/resolve:275:11)
at moduleResolve (node:internal/modules/esm/resolve:932:10)
at defaultResolve (node:internal/modules/esm/resolve:1056:11)
at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:654:12)
at #cachedDefaultResolve (node:internal/modules/esm/loader:603:25)
at ModuleLoader.getModuleJobForRequire (node:internal/modules/esm/loader:353:53)
at new ModuleJobSync (node:internal/modules/esm/module_job:341:34)
at ModuleLoader.importSyncForRequire (node:internal/modules/esm/loader:326:11)
at loadESMFromCJS (node:internal/modules/cjs/loader:1411:24)
at Module._compile (node:internal/modules/cjs/loader:1544:5)
at Object..js (node:internal/modules/cjs/loader:1699:10)
at Module.load (node:internal/modules/cjs/loader:1313:32)
at Function._load (node:internal/modules/cjs/loader:1123:12)
at TracingChannel.traceSync (node:diagnostics_channel:322:14)
at wrapModuleLoad (node:internal/modules/cjs/loader:217:24)
at Module.require (node:internal/modules/cjs/loader:1335:12)
at require (node:internal/modules/helpers:136:16)
at Object.requireModule [as requireOrImport] (C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\mocha\lib\nodejs\esm-utils.js:94:12)
at exports.loadFilesAsync (C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\mocha\lib\nodejs\esm-utils.js:128:34)
at Mocha.loadFilesAsync (C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\mocha\lib\mocha.js:451:19)
at singleRun (C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\mocha\lib\cli\run-helpers.js:164:15)
at exports.runMocha (C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\mocha\lib\cli\run-helpers.js:241:10)
at exports.handler (C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\mocha\lib\cli\run.js:379:11)
at C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\yargs\build\index.cjs:1:8993
at C:\Users\Borewit\IdeaProjects\mocha-esm-issue\node_modules\yargs\build\index.cjs:1:4949 {
code: 'ERR_MODULE_NOT_FOUND',
url: 'file:///C:/Users/Borewit/IdeaProjects/mocha-esm-issue/index.js'
}
Minimal, Reproducible Example
Original regression exposing the issue can be found here: Borewit/music-metadata#2435
I created an Minimum Reproducible Example here: https://github.com/Borewit/mocha-esm-issue
Run
npm install
npm run testVersions
Updating from Mocha version v11.6.0 to v11.7.0
Additional Info
I suspect this issue was introduced in PR #5366.
Issues is not present in Node version ≥ 22.15.0. It is reproducable with 22.14.0 and older.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
status: in triagea maintainer should (re-)triage (review) this issuea maintainer should (re-)triage (review) this issuetype: buga defect, confirmed by a maintainera defect, confirmed by a maintainer
Activity
Borewit commentedon Jun 22, 2025
I also would like to point out that loading ESM with
require()does not support top-levelawait, hence it is not fully ESM compliant.MikaKarjunen commentedon Jun 23, 2025
All of the workarounds I came up with failed. So as far as I'm concerned there's no way forward apart form sticking with the previous version.
voxpelli commentedon Jun 23, 2025
@Borewit That is handled explicitly in the code, its other scenarios that are failing
@MikaKarjunen Sticking with the previous version is the way to go until we fix this.
This is similar to #5380, #5387 and stems from #5366
Borewit commentedon Jun 23, 2025
Did this PR fix the issue?: nodejs/node#55698
legendecas commentedon Jun 23, 2025
Worth highlighting that this issue should go away if upgrading Node.js from v22.x to v22.15 and newer, as mentioned in the OP.
[-]🐛 Bug: Mocha 11.7.6 introduces issues loading ECMAScript Modules (ESM) with `ts-node/esm`[/-][+]🐛 Bug: Mocha 11.7.0 introduces issues loading ECMAScript Modules (ESM) with `ts-node/esm`[/+]JoshuaKGoldberg commentedon Jun 24, 2025
This should be fixed by upgrading to either or both of:
If you're still seeing this error, or other bugs using a new version of Mocha, please do post back in a new issue with a reproduction. Cheers all! 🤎
Borewit commentedon Jun 25, 2025
Thank you all for your prompt support!
I can confirm this issue is resolved with Mocha release v11.7.1.