Skip to content

🐛 Bug: Mocha 11.7.0 introduces issues loading ECMAScript Modules (ESM) with ts-node/esm #5388

Closed

Description

@Borewit

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 faq label, 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 test

to output:

Works fine
  √ ESM TypeScript Test

  1 passing (2ms)

Actual

npm run test

now 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 test

Versions

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.

Activity

  1. added
    type: buga defect, confirmed by a maintainer
    status: in triagea maintainer should (re-)triage (review) this issue
    on Jun 22, 2025
  2. Borewit commented on Jun 22, 2025

    @Borewit
    Author

    I also would like to point out that loading ESM with require() does not support top-level await, hence it is not fully ESM compliant.

  3. MikaKarjunen commented on Jun 23, 2025

    @MikaKarjunen

    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.

  4. voxpelli commented on Jun 23, 2025

    @voxpelli
    Contributor

    I also would like to point out that loading ESM with require() does not support top-level await, hence it not fully ESM compliant.

    @Borewit That is handled explicitly in the code, its other scenarios that are failing

    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.

    @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

  5. Borewit commented on Jun 23, 2025

    @Borewit
    Author

    Did this PR fix the issue?: nodejs/node#55698

  6. legendecas commented on Jun 23, 2025

    @legendecas
    Contributor

    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.

  7. changed the title [-]🐛 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`[/+] on Jun 24, 2025
  8. JoshuaKGoldberg commented on Jun 24, 2025

    @JoshuaKGoldberg
    Member

    This should be fixed by upgrading to either or both of:

    • Mocha >=11.7.1
    • Node.js >=22.15

    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! 🤎

  9. Borewit commented on Jun 25, 2025

    @Borewit
    Author

    Thank you all for your prompt support!

    I can confirm this issue is resolved with Mocha release v11.7.1.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    status: in triagea maintainer should (re-)triage (review) this issuetype: buga defect, confirmed by a maintainer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions