{"record":{"id":"e6363ac84f5667dd","repo":"puppeteer/puppeteer","slug":"browsercontext-is-not-initialized","errorCode":null,"errorMessage":"browserContext is not initialized","messagePattern":"browserContext is not initialized","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/puppeteer-core/src/cdp/Target.ts","lineNumber":174,"sourceCode":"      default:\n        return TargetType.OTHER;\n    }\n  }\n\n  _targetManager(): TargetManager {\n    if (!this.#targetManager) {\n      throw new Error('targetManager is not initialized');\n    }\n    return this.#targetManager;\n  }\n\n  _getTargetInfo(): Protocol.Target.TargetInfo {\n    return this.#targetInfo;\n  }\n\n  override browser(): Browser {\n    if (!this.#browserContext) {\n      throw new Error('browserContext is not initialized');\n    }\n    return this.#browserContext.browser();\n  }\n\n  override browserContext(): BrowserContext {\n    if (!this.#browserContext) {\n      throw new Error('browserContext is not initialized');\n    }\n    return this.#browserContext;\n  }\n\n  override opener(): Target | undefined {\n    const {openerId} = this.#targetInfo;\n    if (!openerId) {\n      return;\n    }\n    return this.browser()\n      .targets()","sourceCodeStart":156,"sourceCodeEnd":192,"githubUrl":"https://github.com/puppeteer/puppeteer/blob/d484e21c17f6023826fefdcdeeb553e04a7aaed8/packages/puppeteer-core/src/cdp/Target.ts#L156-L192","documentation":"Thrown by Target.browser() when #browserContext is undefined. The target has not been associated with a BrowserContext (and therefore a Browser) yet, so it cannot resolve its owning browser. Occurs on targets created before context attachment or detached from their context.","triggerScenarios":"Calling target.browser() on a target captured very early in its lifecycle (before the browser context was assigned), on the top-level browser target itself in some flows, or on a target whose context has been disposed.","commonSituations":"Inspecting targets from a 'targetcreated' listener before the target is fully attached; retaining target references across a browser.close()/disconnect() cycle; calling browser() on orphaned targets in disconnected state.","solutions":["Await target initialization (waitForTarget) before calling .browser().","Check that the browser is still connected before navigating target references.","Re-fetch fresh target references after reconnects rather than reusing pre-disconnect targets."],"exampleFix":"// before\nbrowser.on('targetcreated', t => console.log(t.browser())); // may throw\n\n// after\nbrowser.on('targetcreated', async t => {\n  await new Promise(r => setTimeout(r, 0));\n  if (browser.connected) console.log(t.browser());\n});","handlingStrategy":"validation","validationCode":"if (browser.connected) {\n  try { const b = target.browser(); } catch { /* target not yet bound */ }\n}","typeGuard":"function isAttachedTarget(target): boolean {\n  try { target.browser(); return true; }\n  catch { return false; }\n}","tryCatchPattern":"try { const b = target.browser(); }\ncatch (e) {\n  if (/browserContext is not initialized/.test(e.message)) { /* skip unbound target */ }\n  else throw e;\n}","preventionTips":["Do not retain Target references across browser.close() or disconnect().","In 'targetcreated' listeners, defer reading target.browser() to a microtask/timeout.","Re-fetch targets after a reconnect instead of reusing stale references.","Check browser.connected before traversing the target tree."],"tags":["target","browser-context","lifecycle"],"backgroundTag":null,"analyzedSha":"d484e21c17f6023826fefdcdeeb553e04a7aaed8","analyzedAt":"2026-08-12T06:33:19.665Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}