evanw/esbuild · error · Error

Module not found in bundle: ${path}

Error message

Module not found in bundle: ${path}

What it means

This error is thrown by esbuild's generated `__glob` runtime helper, which is emitted into bundles that use glob-style imports (e.g. `import * from './dir/*.ts'`). The helper holds a static map from request path to a loader function built at bundle time; if at runtime the map is asked for a path that was not present when the bundle was generated, it throws. It is a runtime error in the *generated output*, not a build-time error from esbuild's API.

Source

Thrown at internal/runtime/runtime.go:138

		// shim to fall back to "globalThis.require" even if it's defined later
		// (including property accesses such as "require.resolve") so we need to
		// use a proxy (issue #1614).
		export var __require =
			/* @__PURE__ */ (x =>
				typeof require !== 'undefined' ? require :
				typeof Proxy !== 'undefined' ? new Proxy(x, {
					get: (a, b) => (typeof require !== 'undefined' ? require : a)[b]
				}) : x
			)(function(x) {
				if (typeof require !== 'undefined') return require.apply(this, arguments)
				throw Error('Dynamic require of "' + x + '" is not supported')
			})

		// This is used for glob imports
		export var __glob = map => path => {
			var fn = map[path]
			if (fn) return fn()
			throw new Error('Module not found in bundle: ' + path)
		}

		// For object rest patterns
		export var __restKey = key => typeof key === 'symbol' ? key : key + ''
		export var __objRest = (source, exclude) => {
			var target = {}
			for (var prop in source)
				if (__hasOwnProp.call(source, prop) && exclude.indexOf(prop) < 0)
					target[prop] = source[prop]
			if (source != null && __getOwnPropSymbols)
	`

	// Avoid "of" when not using ES6
	if !unsupportedJSFeatures.Has(compat.ForOf) {
		text += `
				for (var prop of __getOwnPropSymbols(source)) {
		`
	} else {

View on GitHub (pinned to 6ff1d8b0d8)

Solutions

  1. Rebuild the bundle so the glob map includes every path your runtime will request.
  2. Switch from a runtime-constructed path to a literal import so esbuild can statically resolve it, or pre-compute the full set of paths and import each explicitly.
  3. If paths are genuinely dynamic, load them outside the bundle (e.g. fetch at runtime) instead of going through the `__glob` helper.
  4. Inspect the generated bundle's `__glob` map keys to see exactly which paths are available and reconcile against what your code requests.

Example fix

// before
const name = getUserInput()
const mod = __glob(`./icons/${name}.svg`) // may miss

// after
import * as icons from './icons/*'  // esbuild builds a complete map
const mod = icons[`./icons/${name}.svg`]
if (!mod) throw new Error(`unknown icon: ${name}`)
Defensive patterns

Strategy: validation

Validate before calling

// If you control call sites into a generated __glob map, check membership first.
const globMap: Record<string, () => any> = (globalThis as any).__esbuildGlobMap
function safeGlobLoad(path: string) {
  if (!globMap || !(path in globMap)) {
    throw new Error(`Glob import not available at build time: ${path}`)
  }
  return globMap[path]()
}

Type guard

function isKnownGlobPath(map: Record<string, unknown>, p: string): boolean {
  return Object.prototype.hasOwnProperty.call(map, p)
}

Prevention

When it happens

Trigger: Calling the generated `__glob(path)` helper (alias `__globImport`) with a path string that is not a key in the map esbuild produced. This happens when user code or another runtime resolves a dynamic path that wasn't matched by the glob during bundling — e.g. a path produced at runtime via string concatenation, a typo'd suffix, or a file that didn't exist on disk at build time but is requested at runtime.

Common situations: Bundling a glob like `./*.svg` and then trying to import a file added after the bundle was built; constructing the import path dynamically (template literals) so it no longer matches the static keys; migrating from another bundler that resolved globs lazily at runtime; SSR setups that re-evaluate the bundle against a different file tree than the one bundled.

Related errors


AI-assisted analysis of evanw/esbuild@6ff1d8b0d8 (2026-08-03). Data as JSON: /data/errors/2a05a87b3767e413.json. Report an issue: GitHub.