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

Internal assertion in the transform RPC method: it routes raw transforms through the reserved __vitest_vm__ Vite environment, which Vitest registers automatically for in-process module transformation. If that environment is absent, the framework's own setup is broken — explicitly flagged as a bug to report, not a user config issue.

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 d568f8ce37)

Solutions

  1. Report as a Vitest bug with reproduction.
  2. Ensure you are using the standard Vitest entry (vitest/node) and not bypassing its Vite plugin.
  3. Check Vitest and Vite version compatibility and upgrade/downgrade to a matched pair.
  4. Remove custom Vite plugins that mutate server.environments to isolate the cause.
Defensive patterns

Strategy: try-catch

Try / catch

try {
  await vitest.start()
} catch (e) {
  if (e.message.includes('The VM environment was not defined in the Vite config')) {
    console.error('Vitest internal: __vitest_vm__ environment missing. Report as a bug.')
  }
  throw e
}

Prevention

When it happens

Trigger: rpc.transform is called and project.vite.environments.__vitest_vm__ is undefined, at packages/vitest/src/node/pools/rpc.ts:277-279. The __vitest_vm__ environment is created by Vitest's Vite plugin; its absence means the plugin did not register correctly.

Common situations: Vitest Vite plugin not loaded (custom server setup that bypasses the plugin); a severe Vite/Vitest version incompatibility; a plugin that clears environments; an experimental/custom Vitest entrypoint that skips plugin registration.

Related errors


AI-assisted analysis of vitest-dev/vitest@d568f8ce37 (2026-08-03). Data as JSON: /data/errors/65240462fc7baf5f.json. Report an issue: GitHub.