webpack/webpack · error · Error

Automatic publicPath is not supported in this browser

Error message

Automatic publicPath is not supported in this browser

What it means

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.

Source

Thrown at lib/runtime/AutoPublicPathRuntimeModule.js:92

							)})`,
							Template.indent("scriptUrl = document.currentScript.src;"),
							"if (!scriptUrl) {",
							Template.indent([
								`${cst} scripts = document.getElementsByTagName("script");`,
								"if(scripts.length) {",
								Template.indent([
									`${lt} i = scripts.length - 1;`,
									"while (i > -1 && (!scriptUrl || !/^http(s?):/.test(scriptUrl))) scriptUrl = scripts[i--].src;"
								]),
								"}"
							]),
							"}"
						]),
						"}"
					]),
			"// When supporting browsers where an automatic publicPath is not supported you must specify an output.publicPath manually via configuration",
			'// or pass an empty string ("") and set the __webpack_public_path__ variable from your code to use your own logic.',
			'if (!scriptUrl) throw new Error("Automatic publicPath is not supported in this browser");',
			'scriptUrl = scriptUrl.replace(/^blob:/, "").replace(/#.*$/, "").replace(/\\?.*$/, "").replace(/\\/[^\\/]+$/, "/");',
			!undoPath
				? `${RuntimeGlobals.publicPath} = scriptUrl;`
				: `${RuntimeGlobals.publicPath} = scriptUrl + ${JSON.stringify(
						undoPath
					)};`
		]);
	}
}

module.exports = AutoPublicPathRuntimeModule;

View on GitHub (pinned to 318421ea8a)

Solutions

  1. Set an explicit publicPath in the config: output.publicPath = '/' (or your CDN URL) instead of 'auto'.
  2. If you must use 'auto' but runtime-detect, set output.publicPath = '' and assign __webpack_public_path__ from your own code before loading assets.
  3. Ensure the bundle actually runs in a browser/web worker context where the script URL can be discovered.

Example fix

// before (webpack.config.js)
module.exports = { output: { publicPath: 'auto' } };
// after
module.exports = { output: { publicPath: '/' } };
// or, dynamic in app code:
// __webpack_public_path__ = window.appBase + '/assets/';
Defensive patterns

Strategy: validation

Validate before calling

// At build time, prefer an explicit publicPath for non-browser or sandboxed targets
function ensurePublicPath(config) {
  const target = config.target;
  const pp = config.output && config.output.publicPath;
  if (pp === 'auto' && (target === 'node' || target === 'async-node' || target === 'electron-main')) {
    throw new Error('publicPath:"auto" needs a browser/worker runtime; set output.publicPath explicitly for target ' + target);
  }
}

Try / catch

// Inside the bundle, fall back if auto-detection fails
try { /* code that depends on __webpack_public_path__ */ }
catch (e) {
  if (/Automatic publicPath/.test(e.message)) { __webpack_public_path__ = '/assets/'; }
  else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of webpack/webpack@318421ea8a (2026-08-03). Data as JSON: /data/errors/4a60c6cb673f3ff5.json. Report an issue: GitHub.