{"record":{"id":"c34edb9aca22c668","repo":"parcel-bundler/parcel","slug":"external-modules-are-not-supported-when-building-f","errorCode":null,"errorMessage":"External modules are not supported when building for browser","messagePattern":"External modules are not supported when building for browser","errorType":"exception","errorClass":"ThrowableDiagnostic","httpStatus":null,"severity":"error","filePath":"packages/packagers/js/src/ScopeHoistingPackager.js","lineNumber":818,"sourceCode":"    if (\n      this.wrappedAssets.has(asset.id) ||\n      (this.bundle.env.outputFormat === 'commonjs' &&\n        asset === this.bundle.getMainEntry())\n    ) {\n      let exportsName = asset.symbols.get('*')?.local || `$${assetId}$exports`;\n      replacements.set(exportsName, 'module.exports');\n    }\n\n    return [depMap, replacements];\n  }\n\n  addExternal(\n    dep: Dependency,\n    replacements?: Map<string, string>,\n    referencedBundle?: NamedBundle,\n  ) {\n    if (this.bundle.env.outputFormat === 'global') {\n      throw new ThrowableDiagnostic({\n        diagnostic: {\n          message:\n            'External modules are not supported when building for browser',\n          codeFrames: [\n            {\n              filePath: nullthrows(dep.sourcePath),\n              codeHighlights: dep.loc\n                ? [convertSourceLocationToHighlight(dep.loc)]\n                : [],\n            },\n          ],\n        },\n      });\n    }\n\n    let specifier = dep.specifier;\n    if (referencedBundle) {\n      specifier = relativeBundlePath(this.bundle, referencedBundle);","sourceCodeStart":800,"sourceCodeEnd":836,"githubUrl":"https://github.com/parcel-bundler/parcel/blob/59484858a1a0bcbb71f74088956bb437a2db6505/packages/packagers/js/src/ScopeHoistingPackager.js#L800-L836","documentation":"A `ThrowableDiagnostic` thrown by `ScopeHoistingPackager.addExternal` when a dependency is marked external but the bundle's output format is `global` (the default for browser builds). External modules rely on a host module system (CommonJS/ESM), which a `global`/IIFE browser bundle has no way to import — so Parcel refuses rather than emit broken output. The diagnostic includes a code frame at the dependency's source location.","triggerScenarios":"Configuring `externals` (or a package marked external) while targeting a browser global output; setting `--target browser` (global format) with `externals` in package.json; depending on a peer/global that Parcel treats as external under global format.","commonSituations":"Library build with `externals` to exclude a peer dep, but target default resolves to global; migrating a config from esbuild/webpack `external` without switching output format; splitting config per target and forgetting to clear externals for the browser target.","solutions":["Change the bundle's output format to `esmodule` or `commonjs` so externals can be resolved by the host (set in `.parcelrc`/target config or `outputFormat`).","Remove the `externals` entry for the browser/global target.","Bundle the dependency instead of marking it external for global output.","If the global is provided by a script tag, ensure the target is configured for global-output + matching `globals`, or switch formats."],"exampleFix":"// before — package.json\n{\n  \"targets\": { \"browser\": { \"source\": \"src/index.js\" } },\n  \"externals\": { \"react\": \"React\" }\n}\n// after — use esmodule output for externals\n{\n  \"targets\": { \"browser\": { \"source\": \"src/index.js\", \"outputFormat\": \"esmodule\" } },\n  \"externals\": { \"react\": \"React\" }\n}","handlingStrategy":"validation","validationCode":"function assertNoExternalsForGlobal(env, externals) {\n  if (env.outputFormat === 'global' && externals && externals.size > 0) {\n    throw new Error('Externals require esmodule/commonjs output, not global');\n  }\n}","typeGuard":"function isGlobalOutput(env) { return env?.outputFormat === 'global'; }","tryCatchPattern":"try { packager.package(...); }\ncatch (e) {\n  if (/External modules are not supported when building for browser/.test(e.message)) {\n    // switch target.outputFormat to 'esmodule' or remove externals\n  } else throw e;\n}","preventionTips":["Don't combine externals with global/browser output.","Set per-target outputFormat when using externals.","Audit externals when migrating configs between bundlers."],"tags":["parcel","scope-hoisting","externals","browser","bundle","config"],"backgroundTag":null,"analyzedSha":"59484858a1a0bcbb71f74088956bb437a2db6505","analyzedAt":"2026-08-13T04:06:35.925Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}