{"record":{"id":"18956f5c35168a43","repo":"GitbookIO/gitbook","slug":"gitbook-client-not-initialized-call-gitbook-init","errorCode":null,"errorMessage":"GitBook client not initialized. Call GitBook(\"init\", { siteURL: \"...\" }) first.","messagePattern":"GitBook client not initialized\\. Call GitBook\\(\"init\", (.+?)\\) first\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/embed/src/standalone/index.ts","lineNumber":132,"sourceCode":"    document.body.appendChild(probe);\n    const used = getComputedStyle(probe).color;\n    probe.remove();\n\n    return used === 'rgb(255, 255, 255)' ? 'dark' : 'light';\n}\n\n/** Mirror the resolved scheme onto the widget's own chrome, and hand it back for the frame's URL. */\nfunction applyColorScheme(): 'light' | 'dark' {\n    const colorScheme = resolveColorScheme();\n    for (const element of [widgetButton, widgetWindow]) {\n        element.dataset.colorScheme = colorScheme;\n    }\n    return colorScheme;\n}\n\nfunction getClient() {\n    if (!_client) {\n        throw new Error(\n            'GitBook client not initialized. Call GitBook(\"init\", { siteURL: \"...\" }) first.'\n        );\n    }\n    return _client;\n}\n\nfunction getIframe() {\n    if (!widgetIframe || !_frame) {\n        const client = getClient();\n\n        widgetIframe?.remove();\n        widgetIframe = document.createElement('iframe');\n        widgetIframe.id = 'gitbook-widget-iframe';\n        widgetIframe.allow = 'clipboard-write';\n        // One read for both, so the docs can't come back in a different scheme than the panel.\n        widgetIframe.src = client.getFrameURL({ ...frameOptions, colorScheme: applyColorScheme() });\n        widgetWindow.appendChild(widgetIframe);\n","sourceCodeStart":114,"sourceCodeEnd":150,"githubUrl":"https://github.com/GitbookIO/gitbook/blob/db67585ee243d063c459a855988f21612cea9c95/packages/embed/src/standalone/index.ts#L114-L150","documentation":"Thrown by the internal getClient() in @gitbook/embed's standalone bundle when an API is invoked before GitBook('init', ...) has run. The standalone script stores a module-level _client created during 'init'; any other command that needs the client dereferences it and fails if init was skipped or not run yet.","triggerScenarios":"Calling GitBook('getColorScheme') or any client-dependent command before GitBook('init', { siteURL: '...' }); loading the standalone script and immediately issuing commands; init failing silently (e.g. script not fully loaded) so _client is never set.","commonSituations":"Copy-pasting the embed snippet but removing or reordering the init call; race where commands run in a script placed before init; misspelling 'init' or the siteURL option; upgrading @gitbook/embed where the init API surface changed.","solutions":["Ensure GitBook('init', { siteURL: 'https://your-site.gitbook.io/...' }) runs before any other GitBook(...) call","Verify the standalone script is loaded and init is not wrapped in a condition that skips it","If init is async in your flow, await it or issue subsequent commands from its callback/promise"],"exampleFix":"// before\nGitBook('getColorScheme'); // no init yet\n\n// after\nGitBook('init', { siteURL: 'https://docs.example.com/' });\nGitBook('getColorScheme');","handlingStrategy":"validation","validationCode":"let initialized = false;\nif (!initialized) {\n    GitBook('init', { siteURL: 'https://docs.example.com/' });\n    initialized = true;\n}\nGitBook('getColorScheme');","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Make init the first GitBook call in your bootstrap script","Keep init at top-level of the embed snippet, not inside conditional branches","Check the @gitbook/embed docs when upgrading, as init options may change"],"tags":["embed","initialization","standalone","gitbook"],"backgroundTag":"client-not-initialized","analyzedSha":"db67585ee243d063c459a855988f21612cea9c95","analyzedAt":"2026-08-28T17:49:47.831Z","schemaVersion":2},"datasetVersion":"2026-08-28T21:17:43.275Z"}