vitest-dev/vitest · error · Error

The VM environment was not defined in the Vite config. This…

Error message

The VM environment was not defined in the Vite config. This is a bug in Vitest. Please, open a new issue with reproduction.

What it means

An internal invariant: the built-in '__vitest_vm__' DevEnvironment must always exist so transformRequest can run code inside the VM sandbox. Unlike the user-facing environment errors, this one explicitly says it is a bug in Vitest, because the VM environment is created by Vitest itself and should never be absent.

Solutions

  1. Reinstall node_modules (pnpm install) and rebuild Vitest (pnpm build) to rule out a stale/partial build.
  2. Ensure a single Vitest version across the workspace (pnpm why vitest).
  3. Check custom Vite plugins/hooks (configureServer, config) for code that mutates vite.environments.
  4. If reproducible, open a Vitest issue with the reproduction as the message instructs.
Defensive patterns

Strategy: try-catch

Type guard

const hasVmEnvironment = (vite: { environments?: Record<string, unknown> }): boolean =>
  Boolean(vite.environments && '__vitest_vm__' in vite.environments)

Try / catch

try {
  await rpc.transform(id)
} catch (err) {
  if (err instanceof Error && err.message.includes('__vitest_vm__')) {
  // internal Vitest invariant: rebuild/reinstall and report
  throw new Error('Vitest VM environment missing - rebuild Vitest and retry', { cause: err })
  }
  throw err
}

Prevention

When it happens

Trigger: The 'transform' RPC handler reads project.vite.environments.__vitest_vm__ and finds it undefined while transforming a module id (used for in-process VM transforms).

Common situations: A broken Vitest install, partial build, conflicting Vitest versions across the monorepo, or a custom plugin/config that mutates or deletes environments and accidentally removes '__vitest_vm__'. Almost never a normal user-config mistake.

Related errors


AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11). Data as JSON: /api/errors/65240462fc7baf5f. Report an issue: GitHub.

Appendix: source

Thrown at packages/vitest/src/node/pools/rpc.ts:279

      return {
        file: cleanUrl(resolved.id),
        url: normalizeResolvedIdToUrl(environment, resolved.id),
        id: resolved.id,
      }
    },

    snapshotSaved(snapshot) {
      vitest.snapshot.add(snapshot)
    },
    resolveSnapshotPath(testPath: string) {
      return vitest.snapshot.resolvePath<ResolveSnapshotPathHandlerContext>(testPath, {
        config: project.serializedConfig,
      })
    },
    async transform(id) {
      const environment = project.vite.environments.__vitest_vm__
      if (!environment) {
        throw new Error(`The VM environment was not defined in the Vite config. This is a bug in Vitest. Please, open a new issue with reproduction.`)
      }

      const url = normalizeResolvedIdToUrl(environment, fileURLToPath(id))
      const result = await environment.transformRequest(url).catch(handleRollupError)
      return { code: result?.code }
    },
    async onQueued(file) {
      if (methodsOptions.collect) {
        vitest.state.collectFiles(project, [file])
      }
      else {
        await vitest._testRun.enqueued(project, file)
      }
    },
    async onCollected(files) {
      if (methodsOptions.collect) {
        vitest.state.collectFiles(project, files)
      }

View on GitHub (pinned to 1fa9837ec2)