{"id":"36b2a14fadc0a0d9","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. ${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/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/browser/context.js#L1-L21","documentation":"Thrown at module load time by the fallback vitest/browser module (packages/browser/context.js). The real vitest/browser is a virtual module injected only when a test runs in Browser Mode (browser pool). This stub is what Node resolves when something imports vitest/browser from a normal (non-browser) Vitest run, and it throws immediately with a hint about the active pool.","triggerScenarios":"Importing page, userEvent, server, cdp, etc. from 'vitest/browser' (directly or transitively via a shared util) in a test that runs under the forks/threads/vmThreads/vmForks pool instead of the browser pool. The message names the offending pool when one is set.","commonSituations":"A shared helper that imports @vitest/browser is also imported by non-browser unit tests; browser tests not excluded from the default test.include so they run under the regular pool; config that failed to set browser.enabled / browser.provider; CI running the full suite without the browser flag.","solutions":["Run the importing tests in browser mode: vitest --browser (or set projects with browser.enabled).","Exclude browser tests from the regular suite: set test.include so browser files are not picked up by the default pool run, or split projects.","Split shared utils so the @vitest/browser import lives only in browser-only files.","Confirm the active pool is 'browser' for the failing file."],"exampleFix":"// before: shared util imported by both node and browser tests\n// src/shared.ts\nimport { page } from '@vitest/browser' // explodes under node pool\nexport const click = () => page.click('#x')\n\n// after: vitest.config.ts splits projects\nexport default defineConfig({\n  test: {\n    projects: [\n      { test: { name: 'unit', include: ['test/unit/**/*.test.ts'] } },\n      { test: { name: 'browser', include: ['test/browser/**/*.test.ts'], browser: { enabled: true, provider: 'playwright' } } },\n    ],\n  },\n})","handlingStrategy":"validation","validationCode":"const pool = globalThis.__vitest_worker__?.ctx?.pool\nif (pool !== 'browser') {\n  throw new Error(`vitest/browser requires the browser pool; active pool: ${pool}`)\n}\n// then import vitest/browser dynamically inside the test","typeGuard":"function inBrowserPool(): boolean {\n  return globalThis.__vitest_worker__?.ctx?.pool === 'browser'\n}","tryCatchPattern":null,"preventionTips":["Split projects so browser-only files run under the browser pool.","Keep shared utils that import @vitest/browser out of node-pool test files.","Run browser tests with vitest --browser or browser.enabled projects.","Confirm the active pool is 'browser' before importing vitest/browser."],"tags":["browser","import","pool","config","virtual-module","vitest"],"analyzedSha":"d568f8ce3739b532d5bf2c1ee1e45e8a8a473d09","analyzedAt":"2026-08-03T20:23:56.861Z","schemaVersion":2}