{"record":{"id":"a320c1127df487f8","repo":"microsoft/playwright","slug":"failed-to-load-init-page-initpage-reason","errorCode":null,"errorMessage":"Failed to load init page \"${initPage}\": ${reason}","messagePattern":"Failed to load init page \"(.+?)\": (.+?)","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/playwright-core/src/tools/backend/tab.ts","lineNumber":186,"sourceCode":"    const errors = await page.pageErrors().catch(() => []);\n    for (const error of errors)\n      result.push(pageErrorToConsoleMessage(error));\n    return result;\n  }\n\n  private async _initialize() {\n    for (const message of await Tab.collectConsoleMessages(this.page))\n      this._handleConsoleMessage(message);\n    const requests = await this.page.requests().catch(() => []);\n    for (const request of requests.filter(r => r.existingResponse() || r.failure()))\n      this._requests.push(request);\n    for (const initPage of this.context.config.browser?.initPage || []) {\n      try {\n        const { default: func } = require(initPage);\n        await func({ page: this.page });\n      } catch (e) {\n        const reason = e instanceof Error ? e.message : String(e);\n        throw new Error(`Failed to load init page \"${initPage}\": ${reason}`, { cause: e });\n      }\n    }\n  }\n\n  modalStates(): ModalState[] {\n    return this._modalStates;\n  }\n\n  setModalState(modalState: ModalState) {\n    this._modalStates.push(modalState);\n    this.emit(TabEvents.modalState, modalState);\n  }\n\n  clearModalState(modalState: ModalState) {\n    this._modalStates = this._modalStates.filter(state => state !== modalState);\n  }\n\n  private _dialogShown(dialog: playwright.Dialog) {","sourceCodeStart":168,"sourceCodeEnd":204,"githubUrl":"https://github.com/microsoft/playwright/blob/c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6/packages/playwright-core/src/tools/backend/tab.ts#L168-L204","documentation":"Thrown by Tab._initialize when one of the user-configured browser.initPage modules cannot be required or its default export throws. The initPage entries are loaded with require(initPage) and called as func({ page }); any exception is wrapped with the failing module path and original message (cause set).","triggerScenarios":"Configuring context.browser.initPage to a path that does not exist, exports no default function, or whose default function throws during page setup (e.g. page.evaluate failure).","commonSituations":"Custom init scripts that mutate every new page (auth injection, polyfills); a typo in the initPage path; an init script written for a different Playwright API surface; the script throws on about:blank before navigation.","solutions":["Inspect e.cause for the original error from the init module.","Run the init module standalone against a fresh page to reproduce, then fix the script.","Confirm the initPage value resolves via Node's require resolution from the working directory.","Temporarily remove the failing entry from browser.initPage to isolate the failure."],"exampleFix":"// before - init script throws\n// init.js: export default async ({ page }) => { await page.evaluate(() => window.x = undefinedFn()); };\n// config: browser.initPage = ['./init.js']\n\n// after\nexport default async ({ page }) => {\n  await page.addInitScript(() => { window.x = 1; });\n};","handlingStrategy":"try-catch","validationCode":"// Validate each initPage entry resolves before the context is created.\nimport { createRequire } from 'module';\nconst require = createRequire(import.meta.url);\nfunction validateInitPages(entries: string[]) {\n  for (const e of entries) {\n    const mod = require(e);\n    if (typeof mod.default !== 'function')\n      throw new Error(`initPage '${e}' has no default function export`);\n  }\n}","typeGuard":"function isInitFunction(x: unknown): x is (arg: { page: import('playwright-core').Page }) => Promise<void> {\n  return typeof x === 'function';\n}","tryCatchPattern":"try {\n  await context.ensureBrowserContext(); // triggers Tab._initialize\n} catch (e) {\n  const cause = (e as Error & { cause?: Error }).cause;\n  if (e instanceof Error && e.message.startsWith('Failed to load init page')) {\n    // inspect cause, disable the offending initPage entry, retry\n  } else throw e;\n}","preventionTips":["Keep initPage modules side-effect free and resilient to about:blank.","Test each initPage function against a fresh page in isolation.","Use page.addInitScript for one-shot DOM injection rather than imperative code that can throw."],"tags":["tab-init","config","init-script","cause-wrapped"],"backgroundTag":null,"analyzedSha":"c8fc3bf8d31542d59b4d4d9eaab1df93ff541dc6","analyzedAt":"2026-08-12T07:26:36.950Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}