Vitest "failed to access its internal state" - Duplicate Install
Vitest stores per-run state in a single module instance. The error means two different copies of Vitest were loaded - usually a version mismatch or a duplicate in node_modules - or test APIs were called outside the runner.
What this error means
The run aborts with "Vitest failed to access its internal state. One of the following is possible: ..." It often appears after a dependency bump or in a monorepo where packages pull different Vitest versions.
Error: Vitest failed to access its internal state.
One of the following is possible:
- "vitest" is imported directly without running "vitest" command
- "vitest" is imported inside "globalSetup" ...
- Two different versions of "vitest" are loadedCommon causes
Two copies of Vitest in the tree
A hoisting issue or mismatched versions across workspace packages put two vitest installs in node_modules; the runner loads one and your test imports another.
Vitest used outside its runner
Importing from vitest in a plain Node script, or inside globalSetup, runs without the runner’s initialized state and triggers the guard.
How to fix it
Dedupe to a single Vitest version
Align versions and flatten duplicates so exactly one copy is installed.
npm dedupe
npm ls vitest # expect a single resolved version
# pin the same vitest version across workspace packagesOnly use Vitest APIs under the runner
- Run tests via the
vitestcommand, not by executing a file withnode. - Do not import
vitesttest APIs insideglobalSetup- use its provided context instead. - Ensure
viteandvitestversions are compatible.
How to prevent it
- Pin one Vitest version across all workspace packages.
- Run
npm ls vitestin CI to catch duplicate installs. - Invoke tests through the Vitest CLI, never directly with Node.