Skip to content

🐛 Bug: Mocha v11.7.0 fails with error ERR_REQUIRE_ESM #5387

@webJose

Description

@webJose

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

Unit testing runs as per usual.

Actual

Unit tests fail with error ERR_REQUIRE_ESM.

I believe this may have something to do with the Node version, being Node v24 the one under suspicion. However, I could not reproduce consistently.

In short: I have tests run via ts-mocha and mocha v11.7.0. These don't run in the project's CI. The error:

Exception during run: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /home/runner/work/dr-fetch/dr-fetch/tests/DrFetch.test.ts
require() of ES modules is not supported.
require() of /home/runner/work/dr-fetch/dr-fetch/tests/DrFetch.test.ts from /home/runner/work/dr-fetch/dr-fetch/node_modules/mocha/lib/nodejs/esm-utils.js is an ES module file as it is a .ts file whose nearest parent package.json contains "type": "module" which defines all .ts files in that package scope as ES modules.
Instead change the requiring code to use import(), or remove "type": "module" from /home/runner/work/dr-fetch/dr-fetch/package.json.

This comes from a manually-triggered GH action. These are the results. The run was on branch JP/mocha-v11.7.0.

I created another branch from this branch, named JP/mocha-v11.6.0. This branch runs the tests just fine. I tested under Node v20, 22 and 24.

So as to not clutter this description with links, refer to this one and see the runs for the branches mentioned.

Minimal, Reproducible Example

One more note: In my Windows PC, which doesn't have Node v24 and has v22, mocha v11.7.0 works.

Yet one more note: My work PC has never had any version of Node other than v24 (relatively recent installation). In this PC, I have this problem.

Versions

v11.7.0
NodeJS v20, 22, 24

Additional Info

No response

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