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
- 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.
- 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).
- 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.
- 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.
- TypeScript: npm install -D @types/mocha
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.
| Runtime | require("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.0Module 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).
| Versions | Format | First published |
|---|---|---|
| 12.0.0 | ESM-only | Aug 31, 2026 |
| 7.2.0 – 11.8.0 | CommonJS | May 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
- ansi-styles 7.0.0last CJS
5.2.0 - strip-ansi 7.2.0last CJS
6.0.1 - commander 15.0.0last CJS
14.0.3 - supports-color 11.0.0last CJS
8.1.1 - chalk 6.0.0last CJS
4.1.2 - string-width 8.2.2last CJS
4.2.3 - ansi-regex 6.3.0last CJS
5.0.1 - escape-string-regexp 5.0.0last CJS
4.0.0
Similar popularity
- npm 12.0.2CommonJS
- vue 3.5.42Dual
- @rollup/plugin-node-resolve 16.0.3Dual
- graphql-tag 2.12.7CommonJS
- html-webpack-plugin 5.6.8CommonJS
- querystring 0.2.1CommonJS
- postcss-loader 8.2.1CommonJS
- resize-observer-polyfill 1.5.1CommonJS