{"id":"4a60c6cb673f3ff5","repo":"webpack/webpack","slug":"automatic-publicpath-is-not-supported-in-this-brow","errorCode":null,"errorMessage":"Automatic publicPath is not supported in this browser","messagePattern":"Automatic publicPath is not supported in this browser","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"lib/runtime/AutoPublicPathRuntimeModule.js","lineNumber":92,"sourceCode":"\t\t\t\t\t\t\t)})`,\n\t\t\t\t\t\t\tTemplate.indent(\"scriptUrl = document.currentScript.src;\"),\n\t\t\t\t\t\t\t\"if (!scriptUrl) {\",\n\t\t\t\t\t\t\tTemplate.indent([\n\t\t\t\t\t\t\t\t`${cst} scripts = document.getElementsByTagName(\"script\");`,\n\t\t\t\t\t\t\t\t\"if(scripts.length) {\",\n\t\t\t\t\t\t\t\tTemplate.indent([\n\t\t\t\t\t\t\t\t\t`${lt} i = scripts.length - 1;`,\n\t\t\t\t\t\t\t\t\t\"while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;\"\n\t\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\t\"}\"\n\t\t\t\t\t\t\t]),\n\t\t\t\t\t\t\t\"}\"\n\t\t\t\t\t\t]),\n\t\t\t\t\t\t\"}\"\n\t\t\t\t\t]),\n\t\t\t\"// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration\",\n\t\t\t'// or pass an empty string (\"\") and set the __webpack_public_path__ variable from your code to use your own logic.',\n\t\t\t'if (!scriptUrl) throw new Error(\"Automatic publicPath is not supported in this browser\");',\n\t\t\t'scriptUrl = scriptUrl.replace(/^blob:/, \"\").replace(/#.*$/, \"\").replace(/\\\\?.*$/, \"\").replace(/\\\\/[^\\\\/]+$/, \"/\");',\n\t\t\t!undoPath\n\t\t\t\t? `${RuntimeGlobals.publicPath} = scriptUrl;`\n\t\t\t\t: `${RuntimeGlobals.publicPath} = scriptUrl + ${JSON.stringify(\n\t\t\t\t\t\tundoPath\n\t\t\t\t\t)};`\n\t\t]);\n\t}\n}\n\nmodule.exports = AutoPublicPathRuntimeModule;\n","sourceCodeStart":74,"sourceCodeEnd":104,"githubUrl":"https://github.com/webpack/webpack/blob/318421ea8ac81371f5171236a6efb63675576528/lib/runtime/AutoPublicPathRuntimeModule.js#L74-L104","documentation":"This error is thrown by RUNTIME code webpack emits into the bundle (not at build time). When output.publicPath is 'auto', webpack generates code that tries to discover the current script URL via: importScripts (web worker), document.currentScript.src, or import.meta.url, then derives publicPath from it. If none of those is available (no document, no importScripts, no import.meta.url), the generated guard throws this Error so the bundle fails loudly rather than silently using a wrong path.","triggerScenarios":"Running a bundle with publicPath:'auto' in an environment that lacks document.currentScript and importScripts and import.meta.url: e.g. an embedded JS engine, a bare Node eval of a browser bundle, a very old browser, or a non-module bundle loaded in a context without a DOM. Also when the bundle is eval'd or injected in a way that currentScript is null.","commonSituations":"SSR or Node-side require of a browser bundle built with publicPath:'auto'. Loading a non-ESM bundle inside a sandbox/iframe without a script element. Testing the bundle in jsdom/DOMPurify contexts where document.currentScript is unset. Old browsers predating document.currentScript.","solutions":["Set an explicit publicPath in the config: output.publicPath = '/' (or your CDN URL) instead of 'auto'.","If you must use 'auto' but runtime-detect, set output.publicPath = '' and assign __webpack_public_path__ from your own code before loading assets.","Ensure the bundle actually runs in a browser/web worker context where the script URL can be discovered."],"exampleFix":"// before (webpack.config.js)\nmodule.exports = { output: { publicPath: 'auto' } };\n// after\nmodule.exports = { output: { publicPath: '/' } };\n// or, dynamic in app code:\n// __webpack_public_path__ = window.appBase + '/assets/';","handlingStrategy":"validation","validationCode":"// At build time, prefer an explicit publicPath for non-browser or sandboxed targets\nfunction ensurePublicPath(config) {\n  const target = config.target;\n  const pp = config.output && config.output.publicPath;\n  if (pp === 'auto' && (target === 'node' || target === 'async-node' || target === 'electron-main')) {\n    throw new Error('publicPath:\"auto\" needs a browser/worker runtime; set output.publicPath explicitly for target ' + target);\n  }\n}","typeGuard":null,"tryCatchPattern":"// Inside the bundle, fall back if auto-detection fails\ntry { /* code that depends on __webpack_public_path__ */ }\ncatch (e) {\n  if (/Automatic publicPath/.test(e.message)) { __webpack_public_path__ = '/assets/'; }\n  else throw e;\n}","preventionTips":["For SSR/Node/electron-main targets, never use publicPath:'auto'; set an explicit string.","If you need runtime-derived paths, set publicPath:'' and assign __webpack_public_path__ yourself.","Ensure browser bundles run in a real document/web-worker context with a discoverable script URL."],"tags":["runtime","public-path","browser","config"],"analyzedSha":"318421ea8ac81371f5171236a6efb63675576528","analyzedAt":"2026-08-03T19:39:56.731Z","schemaVersion":2}