vitest-dev/vitest · warning · Error
Cannot find environment for
Error message
Cannot find environment for ${testFilePath} What it means
getModuleGraph resolves the Vite DevEnvironment for a test file: it uses the explicitly passed viteEnvironment, else the __vitest__ environment when experimental.viteModuleRunner is false, else getTestFileEnvironment(project, file, browser). If none of these yields an environment, it throws 'Cannot find environment for <file>'. This runs when the UI/API requests the module graph for a file.
Solutions
- Verify browser config (test.browser.enabled with a valid headless provider) if the file is a browser test.
- If experimental.viteModuleRunner is false, ensure the __vitest__ environment is registered in the Vite config.
- Check that test.environmentName / per-project environment is one Vite knows about.
- Restart Vitest after changing environment config so the project's environments map is rebuilt.
Example fix
// before: browser tests but no browser environment registered
export default defineConfig({ test: { browser: { enabled: true } } })
// after
export default defineConfig({
test: { browser: { enabled: true, instances: [{ browser: 'chromium' }] }, projects: [...] }
}) Defensive patterns
Strategy: validation
Validate before calling
const env = viteEnvironment
? project.vite.environments[viteEnvironment]
: getTestFileEnvironment(project, file, browser)
if (!env) throw new Error('no Vite environment registered for ' + file) Type guard
function hasEnvironment(project, name) {
return Boolean(project.vite.environments[name])
} Prevention
- Register all environments you reference in config.
- Keep browser/environment config consistent with the projects list.
- Restart Vitest after environment config changes.
When it happens
Trigger: A request for a module graph (e.g. clicking a file in the Vitest UI, or the API) for a test file whose Vite environment is not registered. Common when browser.enabled but no browser environment is configured, or viteModuleRunner:false is set without the __vitest__ environment being available.
Common situations: Opening the Vitest UI for a project with browser testing misconfigured, a custom Vite environment that is not registered for the file's environment name, or a stale project state after toggling experimental flags.
Related errors
- Failed to import test file
- Not called in the browser
- stackblitz environment does not support the
- The environment was not defined in the Vite config.
- The VM environment was not defined in the Vite config. This…
AI-assisted analysis of vitest-dev/vitest@1fa9837ec2 (2026-08-11).
Data as JSON: /api/errors/9ec1b923fca0d906.
Report an issue: GitHub.
Appendix: source
Thrown at packages/vitest/src/utils/graph.ts:31
const externalized = new Set<string>()
const inlined = new Set<string>()
const project = ctx.getProjectByName(projectName)
const browser = project.config.browser.enabled
let environment: DevEnvironment | undefined
if (viteEnvironment) {
environment = project.vite.environments[viteEnvironment]
}
else {
environment = project.config.experimental.viteModuleRunner === false
? project.vite.environments.__vitest__
: getTestFileEnvironment(project, testFilePath, browser)
}
if (!environment) {
throw new Error(`Cannot find environment for ${testFilePath}`)
}
const seen = new Map<EnvironmentModuleNode, string>()
function get(mod?: EnvironmentModuleNode) {
if (!mod || !mod.id) {
return
}
if (
mod.id === '\0vitest/browser'
// the export helper is injected in all vue files
// so the module graph becomes too bouncy
|| mod.id.includes('plugin-vue:export-helper')
) {
return
}
if (seen.has(mod)) {
return seen.get(mod)
}View on GitHub (pinned to 1fa9837ec2)