canirequire

canirequireESM-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. 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 you can require() on every Node.js version, including 18 and older, is 11.8.0, published Aug 2, 2026.

On Node.js 20.19+, 22.12+ and every newer release, require("mocha") works anyway: Node can load synchronous ES modules through require(). Verified on Node 20, 22, 24, 26. Node.js 18 and older still fail with ERR_REQUIRE_ESM.

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.

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✓ works✓ works

How to import mocha

ES modules (import)

import mocha from "mocha";

CommonJS (require)

// Node.js 20.19+ / 22.12+ / newer (package has no top-level await):
const mocha = require("mocha");

// Any Node.js version:
const mocha = await import("mocha");

// Or pin the last CommonJS release:
// npm install mocha@11.8.0

Module format by version

Derived from the type, main and exports fields of every stable release on npm (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 you can require() on every Node.js version, including 18 and older, is 11.8.0, published Aug 2, 2026.

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

11.8.0 is the last version of mocha with a CommonJS build. 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?

Use a dynamic import: const mod = await import("mocha"). On Node.js 20.19+, 22.12+ and newer, a plain require("mocha") also works for ESM packages that do not use top-level await. Alternatively pin mocha@11.8.0.

Does mocha work in Bun?

Yes. import "mocha" loaded successfully in Bun 1.4 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