{"id":"69ae70eb3c40eeeb","repo":"vitest-dev/vitest","slug":"vitest-browser-can-be-imported-only-inside-the-bro-69ae70","errorCode":null,"errorMessage":"vitest/browser can be imported only inside the Browser Mode. ${pool ? `Your test is running in ${pool} pool. Make sure your regular tests are excluded from the \"test.include\" glob pattern.` : 'Instead, it was imported outside of Vitest.'}","messagePattern":"vitest/browser can be imported only inside the Browser Mode\\. (.+?) pool\\. Make sure your regular tests are excluded from the \"test\\.include\" glob pattern\\.` : 'Instead, it was imported outside of Vitest\\.'\\}","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/vitest/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/d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09/packages/vitest/browser/context.js#L1-L21","documentation":"Thrown at module-evaluation time in packages/vitest/browser/context.js — the file is the stub Vitest serves whenever a test imports 'vitest/browser' but is not actually running in the browser pool. The stub reads the current worker's pool name and throws, tailoring the message: if a pool is set, it tells you the test is running in the wrong pool (e.g. forks/threads) and that your regular tests are catching the browser include glob; otherwise it notes the import happened outside Vitest entirely.","triggerScenarios":"import { page, userEvent } from 'vitest/browser' in a test that runs under the default (threads/forks) pool; a shared utility imported by both Node tests and browser tests that pulls from 'vitest/browser'; test.include glob broad enough to feed browser-only specs into the regular runner.","commonSituations":"Enabling browser mode without excluding browser specs from the regular test run; sharing helper modules across Node and browser tests; importing the browser API in setup files that also run for non-browser tests.","solutions":["Split test.include into separate globs for browser and Node tests, or add an env/projects config so browser specs only run under the browser project.","Move 'vitest/browser' imports out of shared utilities and into browser-only test files.","Run browser tests with a dedicated project: defineProjects([{ test: { name: 'browser', browser: { enabled: true, instances: [...] }, include: ['**/*.browser.test.ts'] } }]).","If you imported it outside Vitest, remove the import — the browser API is only meaningful under the browser provider."],"exampleFix":"// vitest.config.ts — before\ntest: { include: ['**/*.test.ts'], browser: { enabled: true, instances: [{ browser: 'chromium' }] } }\n\n// after — separate projects\nexport default defineConfig({\n  test: {\n    projects: [\n      { test: { name: 'node', include: ['test/**/*.node.test.ts'] } },\n      { test: { name: 'browser', include: ['test/**/*.browser.test.ts'], browser: { enabled: true, instances: [{ browser: 'chromium' }], provider: playwright() } } },\n    ],\n  },\n})","handlingStrategy":"validation","validationCode":"function isBrowserPool(): boolean {\n  return globalThis.__vitest_worker__?.ctx?.pool === 'browser'\n}\nif (!isBrowserPool()) {\n  throw new Error('vitest/browser imports require the browser pool')\n}","typeGuard":"interface VitestWorker { ctx?: { pool?: string } }\nfunction isBrowserWorker(g: typeof globalThis): g is typeof globalThis & { __vitest_worker__: VitestWorker } {\n  return (g as any).__vitest_worker__?.ctx?.pool === 'browser'\n}","tryCatchPattern":null,"preventionTips":["Split test.include into browser-only and node-only globs via projects config.","Keep 'vitest/browser' imports out of shared utilities consumed by node tests.","Use vitest projects to isolate browser specs under the browser provider."],"tags":["browser","configuration","pool","include-glob"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}