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 showFix it fast
Most common cause, 1 of 3A test or setup file calls require('vitest')
Switch the file to import syntax.
// 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 } })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.
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).
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:
- 1A test or setup file calls require('vitest')
grep finds require('vitest') in a *.test.js, *.spec.cjs or a file listed in setupFiles - 2Jest, not Vitest, is running a file that imports from vitest
The stack trace shows jest-runtime or babel-jest frames, or the npm test script calls jest - 3A shared helper package was built to CommonJS and imports vitest
grep 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.
npm view vitest exports --jsonFix
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.
// 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.
npx vitest runConfirmed by vitest.dev
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.
npm pkg get scripts.test && ls jest.config.* 2>/dev/nullFix
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.
npm pkg set scripts.test="vitest run"
npm testVerify
npm test prints the Vitest banner and the test summary.
npm testConfirmed by jestjs.io
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.
grep -rn "require(\"vitest\")" packages/*/dist 2>/dev/null | headFix
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.
// 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.
npx vitest runConfirmed by github.com
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.