Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Next.js and React

A test run stops because something loads Vitest with require()

Your error

Error: Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead.1 more line it can show

Fix it fast

Most common cause, 1 of 3

A test or setup file calls require('vitest')

Switch the file to import syntax.

Fix
// before: const { describe, it, expect } = require('vitest')
import { describe, it, expect } from 'vitest'

// or, vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({ test: { globals: true } })
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

Vitest cannot be imported in a CommonJS module using require() comes from the vitest package's CommonJS entry, which exists only to block require('vitest'). Something loaded Vitest the CommonJS way: a test or setup file using require, Jest transforming imports to CommonJS, or a helper package built as CommonJS. Use import, run the file with vitest, or emit ESM.

  • Applies to Vitest 5.1.1, Node.js 22.12+ and 24, Jest 30.5 (when both runners are installed)
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Search for literal require calls on vitest outside the vitest package itself. A hit in your test or setup files is cause 1, a hit in a built dist folder is cause 3, and no hit at all usually means another runner rewrote your imports (cause 2).

Run this first
grep -rnE "require\(['\"]vitest['\"]\)" --include=*.js --include=*.cjs --include=*.ts --exclude-dir=.git . | grep -v node_modules/vitest/

Then find the line you see in this list and open its fix:

  1. 1A test or setup file calls require('vitest')grep finds require('vitest') in a *.test.js, *.spec.cjs or a file listed in setupFiles
  2. 2Jest, not Vitest, is running a file that imports from vitestThe stack trace shows jest-runtime or babel-jest frames, or the npm test script calls jest
  3. 3A shared helper package was built to CommonJS and imports vitestgrep finds require("vitest") inside a dist/ folder or a workspace package under node_modules, not in your own sources

What does the error look like?

  • Error: Vitest cannot be imported in a CommonJS module using require(). Please use "import" instead.
  • If you are using "import" in your source code, then it's possible it was bundled into require() automatically by your bundler.

Cause 1 of 3

A test or setup file calls require('vitest')

You seegrep finds require('vitest') in a *.test.js, *.spec.cjs or a file listed in setupFiles

Check

The vitest package maps its require condition to index.cjs, a file whose only job is to throw the cannot-be-imported-in-CommonJS error. Vitest's own guide calls the tool ESM-first and says require() calls bypass Vite and go straight to Node.js, so require('vitest') always lands on that guard.

Check
npm view vitest exports --json

Fix

Switch the file to import syntax. If you are porting Jest tests and want describe, it and expect without imports, turn on globals in the Vitest config instead.

Fix
// before: const { describe, it, expect } = require('vitest')
import { describe, it, expect } from 'vitest'

// or, vitest.config.ts
import { defineConfig } from 'vitest/config'
export default defineConfig({ test: { globals: true } })

Verify

The suite starts and reports test results instead of the guard error.

Verify
npx vitest run

Confirmed by vitest.dev

Did this fix it?

Cause 2 of 3

Jest, not Vitest, is running a file that imports from vitest

You seeThe stack trace shows jest-runtime or babel-jest frames, or the npm test script calls jest

Check

When the error appears even though your file uses import, check which runner started it. Jest's ESM page says its transformer emits CommonJS by default, so an import from vitest is rewritten into require('vitest') before it runs, which hits the guard. A leftover jest.config file or a Jest-based editor runner can start it.

Check
npm pkg get scripts.test && ls jest.config.* 2>/dev/null

Fix

Run these tests with Vitest only. Point the test script at vitest, remove the leftover Jest config, and switch the editor's test runner extension to Vitest.

Fix
npm pkg set scripts.test="vitest run"
npm test

Verify

npm test prints the Vitest banner and the test summary.

Verify
npm test

Confirmed by jestjs.io

Did this fix it?

Cause 3 of 3

A shared helper package was built to CommonJS and imports vitest

You seegrep finds require("vitest") inside a dist/ folder or a workspace package under node_modules, not in your own sources

Check

The second half of the error message describes this case: source code that says import was bundled into require() by the build tool. Test utilities compiled with a CommonJS target turn every import from vitest into a require call that can never work.

Check
grep -rn "require(\"vitest\")" packages/*/dist 2>/dev/null | head

Fix

Build the helper package as ESM, or keep vitest out of its bundle and load it with dynamic import(), which the error text names as the other option for CommonJS modules.

Fix
// inside a CommonJS helper
async function getExpect() {
  const { expect } = await import('vitest')
  return expect
}

Verify

Rebuild the helper, run the suite again, and grep no longer finds require("vitest") in its dist output.

Verify
npx vitest run

Confirmed by github.com

Did this fix it?

How do I stop it coming back?

  • Keep one test runner per package; delete jest, babel-jest and jest.config files when the move to Vitest is done.
  • Publish shared test helpers as ESM ("type": "module") so they can import vitest directly.
  • Add a lint rule or CI grep that fails on require('vitest') in test and setup files.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Workforce.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.