{"record":{"id":"8178f198e657f172","repo":"vitest-dev/vitest","slug":"vitest-browser-can-be-imported-only-inside-the-bro-8178f1","errorCode":null,"errorMessage":"vitest/browser can be imported only inside the Browser Mode. Instead, it was imported outside of Vitest.","messagePattern":"vitest/browser can be imported only inside the Browser Mode\\. 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/1fa9837ec26533512fdcad8baebf249771bd340a/packages/vitest/browser/context.js#L1-L21","documentation":"Same module-level guard as error 167, but this branch fires when globalThis.__vitest_worker__ is absent entirely (no Vitest worker context), meaning vitest/browser was imported outside of any Vitest run — e.g. imported directly by Node, by a plain script, or during SSR/build of an application that bundled the test entry.","triggerScenarios":"Importing vitest/browser in a non-test context: a production build that accidentally includes test files, a Node script that imports a test helper, or a bundler resolving vitest/browser during app compilation.","commonSituations":"Test files leaking into the production build glob; a shared utility imported by both app code and tests that pulls in vitest/browser; running test files with plain node/tsx instead of vitest.","solutions":["Ensure vitest/browser is only imported from files collected by Vitest's browser pool, never from app or build code.","Exclude test directories from your bundler/build entry points.","Run test files through the vitest CLI, not node/tsx directly."],"exampleFix":"// before — app code transitively imports a helper that imports vitest/browser\n\n// after — split test-only helpers into a separate module the app never imports,\n// or guard the import:\nif (import.meta.env?.MODE === 'test') {\n  const { page } = await import('vitest/browser')\n}","handlingStrategy":"validation","validationCode":"const ctx = globalThis.__vitest_worker__\nif (!ctx?.ctx?.pool) {\n  // not inside Vitest — do not import vitest/browser\n}","typeGuard":"function isInsideVitestWorker(): boolean {\n  return !!globalThis.__vitest_worker__?.ctx\n}","tryCatchPattern":null,"preventionTips":["Never import vitest/browser from application or build code.","Exclude test directories from bundler entry points.","Run test files via the vitest CLI only."],"tags":["browser","import-guard","build-config","environment"],"backgroundTag":null,"analyzedSha":"1fa9837ec26533512fdcad8baebf249771bd340a","analyzedAt":"2026-08-11T16:11:39.638Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}