canirequire

canirequire › ESM-only › mocha

Is mocha ESM-only? Can you require() it?

Classic, reliable, trusted test framework for Node.js and the browser

Last verified Sep 9, 2026 against mocha@12.0.0. Markdown · JSON

ESM-only ESM-only since 12.0.0 — last CommonJS version is 11.8.0

mocha dropped its CommonJS build in 12.0.0 (August 2026). The last version with a plain require()-able CommonJS entry is 11.8.0, published Aug 2, 2026; it declares support for Node.js ^18.18.0 || ^20.9.0 || >=21.1.0.

On Node.js 20.19+, 22.12+ and newer, require("mocha") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 20, 22, 24 and 26. Node.js 18 has no require(esm), and mocha@12.0.0 declares Node.js ^20.19.0 || >=22.12.0, so 18 is outside its supported range anyway.

mocha@12.0.0 declares engines "^20.19.0 || >=22.12.0", so Node.js 18 is outside its supported range regardless of module format.

What to do

  1. On Node.js 20.19+, 22.12+ and newer, require("mocha") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 20, 22, 24 and 26.
  2. From a CommonJS file, use a dynamic import inside an async function: const mocha = await import("mocha"). That works on every Node.js version the package supports (^20.19.0 || >=22.12.0).
  3. If you need a synchronous require() or run Node.js 18: npm install mocha@11.8.0, the last CommonJS release (published Aug 2, 2026); it declares Node.js ^18.18.0 || ^20.9.0 || >=21.1.0.
  4. mocha@12.0.0 declares engines "^20.19.0 || >=22.12.0", so Node.js 18 is unsupported regardless: upgrade Node.js or pin an older release.
  5. TypeScript: npm install -D @types/mocha
Latest
12.0.0 (Aug 31, 2026)
Module format
ESM-only
Last CommonJS version
11.8.0
ESM-only since
12.0.0 (Aug 31, 2026)
TypeScript types
@types/mocha
Node engines
^20.19.0 || >=22.12.0
Weekly downloads
13.3M
License
MIT

Does mocha@12.0.0 actually load? Runtime test results

Installed and executed on Sep 9, 2026 with require("mocha") from a CommonJS file and import "mocha" from an ES module.

Runtimerequire("mocha")import "mocha"
Node.js 18✗ failsERR_REQUIRE_ESM✗ failsERR_REQUIRE_ESM
Node.js 20✓ works✓ works
Node.js 22✓ works✓ works
Node.js 24✓ works✓ works
Node.js 26✓ works✓ works
Bun 1.4.0✓ works✓ works

How to import mocha

ES modules (import)

import * as mocha from "mocha";

Namespace import shown because the ESM entry may have no default export; use named imports for what it exports.

CommonJS (require)

// Node.js 20.19+, 22.12+ and newer (verified):
const mocha = require("mocha");

// Any Node.js version the package supports (^20.19.0 || >=22.12.0), from an async function:
async function main() {
  const mocha = await import("mocha");
}

// Or pin the last CommonJS release (declares Node.js ^18.18.0 || ^20.9.0 || >=21.1.0):
// npm install mocha@11.8.0

Module format by version

Derived from the type, main and exports fields of every stable release on npm, corrected where a runtime test of that version contradicted them (209 versions; the newest 60 shown).

VersionsFormatFirst published
12.0.0ESM-onlyAug 31, 2026
7.2.0 – 11.8.0CommonJSMay 23, 2020

Frequently asked

Is mocha ESM-only?

mocha@12.0.0 is ESM-only. mocha dropped its CommonJS build in 12.0.0 (August 2026). The last version with a plain require()-able CommonJS entry is 11.8.0, published Aug 2, 2026; it declares support for Node.js ^18.18.0 || ^20.9.0 || >=21.1.0.

Which version of mocha still supports CommonJS / require()?

11.8.0 is the last version of mocha with a CommonJS build; it declares Node.js ^18.18.0 || ^20.9.0 || >=21.1.0. Install it with "npm install mocha@11.8.0". Every version from 12.0.0 on is ESM-only.

How do I use mocha from a CommonJS file?

From a CommonJS file, use a dynamic import inside an async function: const mocha = await import("mocha"). That works on every Node.js version the package supports (^20.19.0 || >=22.12.0). On Node.js 20.19+, 22.12+ and newer, require("mocha") works with the latest version: Node.js loads synchronous ES modules through require(). Verified on Node.js 20, 22, 24 and 26. Alternatively pin mocha@11.8.0 (declares Node.js ^18.18.0 || ^20.9.0 || >=21.1.0).

Does mocha work in Bun?

Yes. import "mocha" loaded successfully in Bun 1.4.0 on Sep 9, 2026.

Does mocha ship TypeScript types?

Not bundled. Install the community types with "npm install -D @types/mocha".

Links

Other esm-only packages

Similar popularity