{"record":{"id":"64b57332cbb967fc","repo":"vitest-dev/vitest","slug":"vitest-browser-can-be-imported-only-inside-the-bro","errorCode":null,"errorMessage":"vitest/browser can be imported only inside the Browser Mode. Your test is running in ${pool} pool. Make sure your regular tests are excluded from the \"test.include\" glob pattern.","messagePattern":"vitest/browser can be imported only inside the Browser Mode\\. Your test is running in (.+?) pool\\. Make sure your regular tests are excluded from the \"test\\.include\" glob pattern\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/browser/context.js","lineNumber":14,"sourceCode":"// Vitest resolves \"vitest/browser\" as a virtual module instead\n\n// fake exports for static analysis\nexport const page = null\nexport const server = null\nexport const userEvent = null\nexport const cdp = null\nexport const commands = null\nexport const locators = null\nexport const utils = null\n\nconst pool = globalThis.__vitest_worker__?.ctx?.pool\n\nthrow new Error(\n  // eslint-disable-next-line prefer-template\n  'vitest/browser can be imported only inside the Browser Mode. '\n  + (pool\n    ? `Your test is running in ${pool} pool. Make sure your regular tests are excluded from the \"test.include\" glob pattern.`\n    : 'Instead, it was imported outside of Vitest.'),\n)\n","sourceCodeStart":1,"sourceCodeEnd":21,"githubUrl":"https://github.com/vitest-dev/vitest/blob/1fa9837ec26533512fdcad8baebf249771bd340a/packages/browser/context.js#L1-L21","documentation":"`vitest/browser` is a virtual module resolved only when the test runs under the browser pool (the tester sets globalThis.__vitest_worker__.ctx.pool = 'browser'). The fallback context.js throws on import; if a pool is present but is not 'browser', the message names the offending pool. This prevents accidentally using browser-only APIs (page, userEvent, cdp) in node-run tests.","triggerScenarios":"A file imports `vitest/browser` (directly or transitively via a component library or shared util) and runs under `forks`, `threads`, `vmThreads`, or `typescript` pool. Typically because the file is matched by both a node test project and the browser project's include globs.","commonSituations":"Shared util/component imported by both node and browser specs; include globs too broad; a dependency unconditionally imports `vitest/browser`; typecheck pool trying to evaluate the module.","solutions":["Narrow the node project's `include` / the browser project's `include` so the same file is not in both.","Make the `vitest/browser` import lazy/conditional (dynamic import inside a function only called from browser tests).","Exclude browser specs from the node project's `test.include` (and vice versa) using separate project configs.","If a third-party dep imports it unconditionally, report upstream or alias the module for non-browser projects."],"exampleFix":"// before - vitest.config.ts single project catches everything\nexport default defineConfig({ test: { include: ['**/*.test.ts'] } })\n\n// after - split projects by environment\nexport default defineConfig({\n  test: {\n    workspace: [\n      { test: { name: 'unit', include: ['test/unit/**/*.test.ts'] } },\n      { test: { name: 'browser', include: ['test/browser/**/*.test.ts'], browser: { provider: 'playwright' } } },\n    ],\n  },\n})","handlingStrategy":"validation","validationCode":"// split workspace projects so the same file is not in both node and browser pools\nexport default defineConfig({\n  test: {\n    workspace: [\n      { test: { name: 'unit', include: ['test/unit/**/*.test.ts'] } },\n      { test: { name: 'browser', include: ['test/browser/**/*.test.ts'], browser: { provider: 'playwright' } } },\n    ],\n  },\n})","typeGuard":"// runtime guard inside shared code\nfunction isBrowserPool(): boolean {\n  return globalThis.__vitest_worker__?.ctx?.pool === 'browser'\n}\n\nif (isBrowserPool()) {\n  // safe to use vitest/browser APIs here\n}","tryCatchPattern":null,"preventionTips":["Use disjoint include globs between browser and node projects.","Keep vitest/browser imports in test files, not in shared utils imported by node specs.","Use a runtime pool check before dynamic-importing browser-only code."],"tags":["browser","config","pool","import","workspace"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}