{"record":{"id":"47d3129d7f0b7b9b","repo":"projectdiscovery/katana","slug":"could-not-initialize-javascript-env","errorCode":null,"errorMessage":"could not initialize javascript env","messagePattern":"could not initialize javascript env","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/engine/headless/browser/browser.go","lineNumber":467,"sourceCode":"\tbrowserPage := &BrowserPage{\n\t\tPage:        page,\n\t\tBrowser:     browser,\n\t\tlauncher:    l,\n\t\tcancel:      cancel,\n\t\tuserDataDir: tempDir,\n\t}\n\tif err := browserPage.handlePageDialogBoxes(); err != nil {\n\t\treturn nil, err\n\t}\n\n\t// Add stealth evasion JS\n\t_, err = page.EvalOnNewDocument(stealth.JS)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"could not initialize stealth\")\n\t}\n\terr = js.InitJavascriptEnv(page)\n\tif err != nil {\n\t\treturn nil, errors.Wrap(err, \"could not initialize javascript env\")\n\t}\n\n\t// Success - cancel any deferred cleanup\n\tsuccessfulPageCreation = true\n\treturn browserPage, nil\n}\n\n// GetPageFromPool returns a page from the pool\nfunc (l *Launcher) GetPageFromPool() (*BrowserPage, error) {\n\tbrowserPage, err := l.browserPool.Get(l.createBrowserPageFunc)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\t// TODO: should we check if the browser is alive because sometimes it\n\t// might die?\n\treturn browserPage, nil\n}\n","sourceCodeStart":449,"sourceCodeEnd":485,"githubUrl":"https://github.com/projectdiscovery/katana/blob/e3e742739c3746f085943ce918fb4e2b8daf6fe6/pkg/engine/headless/browser/browser.go#L449-L485","documentation":"createBrowserPageFunc wraps js.InitJavascriptEnv(page) failure with this message. InitJavascriptEnv sets up katana's in-page JavaScript execution environment (helpers exposed to page scripts / hooking) by evaluating setup code in the page. Failure means the CDP evaluation against the page failed — typically because the page's CDP session or browser connection is no longer valid.","triggerScenarios":"js.InitJavascriptEnv(page) returning an error when its internal page.Eval/Evaluate calls fail due to a closed target, cancelled context (e.g. the cancelCtx created at browser.go:446 was already cancelled), browser crash, or an invalid/unresponsive execution context.","commonSituations":"Pages whose context was cancelled because an enclosing crawl deadline expired during setup; Chrome crash under memory pressure between stealth init and JS env init; navigation racing with setup invalidating the execution context ('Execution context was destroyed' errors); remote browser disconnects.","solutions":["Read the wrapped error: 'Execution context was destroyed' means the page navigated during setup — retry page creation; 'target closed' means the browser died — relaunch it.","Ensure the page context/cancelCtx is not already cancelled before initialization (check page.GetContext().Err()).","Retry the whole createBrowserPageFunc call rather than individual steps, since a partially initialized page is discarded by its deferred cleanup.","Update katana/rod if the JS env initialization conflicts with the current Chrome protocol version."],"exampleFix":"// before: ignoring context state before init\nif err := js.InitJavascriptEnv(page); err != nil { return nil, err }\n\n// after: bail out early on a dead context and recreate\nif err := page.GetContext().Err(); err != nil {\n    return nil, fmt.Errorf(\"page context dead, recreating: %w\", err)\n}\nif err := js.InitJavascriptEnv(page); err != nil {\n    return recreateBrowserPage()\n}","handlingStrategy":"retry","validationCode":"if err := page.GetContext().Err(); err != nil {\n    return fmt.Errorf(\"page context cancelled before js env init: %w\", err)\n}","typeGuard":null,"tryCatchPattern":"if err := js.InitJavascriptEnv(page); err != nil {\n    if strings.Contains(err.Error(), \"context canceled\") || strings.Contains(err.Error(), \"target closed\") {\n        return recreateBrowserPage() // retry full page creation once\n    }\n    return err\n}","preventionTips":["Ensure crawl deadlines exceed page-setup time so cancelCtx isn't fired during init.","Recreate whole pages rather than re-running individual init steps.","Watch for rapid navigation during setup; consider waiting for load before init.","Keep protocol definitions in sync with the installed Chrome version."],"tags":["headless-browser","javascript","cdp","rod"],"backgroundTag":"execution-context-destroyed","analyzedSha":"e3e742739c3746f085943ce918fb4e2b8daf6fe6","analyzedAt":"2026-09-03T14:55:13.248Z","contentChangedAt":"2026-09-03T14:55:13.248Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}