canirequire › ESM-only › vitest
Is vitest ESM-only? Can you require() it?
Next generation testing framework powered by Vite
Last verified Sep 9, 2026 against vitest@5.0.0. Markdown · JSON
ESM-only ESM-only since 1.0.0 — last CommonJS version is 0.34.6
vitest dropped its CommonJS build in 1.0.0 (December 2023). The last version with a plain require()-able CommonJS entry is 0.34.6, published Sep 29, 2023; it declares support for Node.js >=v14.18.0.
require("vitest") fails on every Node.js version we test, even Node.js 22.12+ and newer where require(esm) exists: the CommonJS entry declared by vitest@5.0.0 exists only to throw (Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead). That includes Node.js 18: no Node.js version can load vitest@5.0.0 with a synchronous require().
vitest@5.0.0 declares engines "^22.12.0 || ^24.0.0 || >=26.0.0", so Node.js 18, 20 are outside its supported range regardless of module format.
What to do
- require("vitest") fails on every Node.js version we test, even Node.js 22.12+ and newer where require(esm) exists: the CommonJS entry declared by vitest@5.0.0 exists only to throw (Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead).
- From a CommonJS file, use a dynamic import inside an async function: const vitest = await import("vitest"). That works on every Node.js version the package supports (^22.12.0 || ^24.0.0 || >=26.0.0).
- If you need a synchronous require() or run Node.js 18: npm install vitest@0.34.6, the last CommonJS release (published Sep 29, 2023); it declares Node.js >=v14.18.0.
- vitest@5.0.0 declares engines "^22.12.0 || ^24.0.0 || >=26.0.0", so Node.js 18, 20 are unsupported regardless: upgrade Node.js or pin an older release.
- TypeScript: types are bundled; install nothing extra.
Does vitest@5.0.0 actually load? Runtime test results
Installed and executed on Sep 9, 2026 with require("vitest") from a CommonJS file and import "vitest" from an ES module.
| Runtime | require("vitest") | import "vitest" |
|---|---|---|
| Node.js 18 | ✗ failsError | ✓ works |
| Node.js 20 | ✗ failsError | ✓ works |
| Node.js 22 | ✗ failsError | ✓ works |
| Node.js 24 | ✗ failsError | ✓ works |
| Node.js 26 | ✗ failsError | ✓ works |
| Bun 1.4.0 | ✗ failsError | ✓ works |
How to import vitest
ES modules (import)
import * as vitest from "vitest";Namespace import shown because the ESM entry may have no default export; use named imports for what it exports.
CommonJS (require)
// require("vitest") fails on every Node.js version we test: the CommonJS entry only throws (Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead).
// Any Node.js version the package supports (^22.12.0 || ^24.0.0 || >=26.0.0), from an async function:
async function main() {
const vitest = await import("vitest");
}
// Or pin the last CommonJS release (declares Node.js >=v14.18.0):
// npm install vitest@0.34.6Module 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 (410 versions; the newest 60 shown).
| Versions | Format | First published |
|---|---|---|
| 2.1.5 – 5.0.0 | ESM-only | Nov 13, 2024 |
Frequently asked
Is vitest ESM-only?
vitest@5.0.0 is ESM-only. vitest dropped its CommonJS build in 1.0.0 (December 2023). The last version with a plain require()-able CommonJS entry is 0.34.6, published Sep 29, 2023; it declares support for Node.js >=v14.18.0.
Which version of vitest still supports CommonJS / require()?
0.34.6 is the last version of vitest with a CommonJS build; it declares Node.js >=v14.18.0. Install it with "npm install vitest@0.34.6". Every version from 1.0.0 on is ESM-only.
How do I use vitest from a CommonJS file?
From a CommonJS file, use a dynamic import inside an async function: const vitest = await import("vitest"). That works on every Node.js version the package supports (^22.12.0 || ^24.0.0 || >=26.0.0). require("vitest") fails on every Node.js version we test, even Node.js 22.12+ and newer where require(esm) exists: the CommonJS entry declared by vitest@5.0.0 exists only to throw (Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead). Alternatively pin vitest@0.34.6 (declares Node.js >=v14.18.0).
Does vitest work in Bun?
Yes. import "vitest" loaded successfully in Bun 1.4.0 on Sep 9, 2026.
Does vitest ship TypeScript types?
Yes. vitest bundles its own type declarations; no @types package is needed.
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
- @vitest/utils 5.0.0ESM-only
- log-symbols 7.0.1ESM-only
- express 5.2.1CommonJS
- async 3.2.6CommonJS
- prop-types 15.8.1CommonJS
- @typescript-eslint/eslint-plugin 8.70.0CommonJS
- forwarded 0.2.0CommonJS
- setprototypeof 1.2.0CommonJS