Skip to content
Latchkey

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.

Vitest output
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 loaded

Common 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.

Terminal
npm dedupe
npm ls vitest   # expect a single resolved version
# pin the same vitest version across workspace packages

Only use Vitest APIs under the runner

  1. Run tests via the vitest command, not by executing a file with node.
  2. Do not import vitest test APIs inside globalSetup - use its provided context instead.
  3. Ensure vite and vitest versions are compatible.

How to prevent it

  • Pin one Vitest version across all workspace packages.
  • Run npm ls vitest in CI to catch duplicate installs.
  • Invoke tests through the Vitest CLI, never directly with Node.

Frequently asked questions

What causes ""failed to access its internal state""?
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.
How do I fix "failed to access its internal state"?
Align versions and flatten duplicates so exactly one copy is installed.

Related guides

References

Latchkey auto-heals failures like this one - detected, fixed, and retried without you. Start free → 30-day trial · No credit card