Skip to content

Webpack doesn't unbox externals when libraryTarget is amd and package.json type field is explicitly "module" #20383

@betalb

Description

@betalb

Bug Description

Given:

  1. a project that explicitly specifies "type": "module" in package.json;
  2. a source code in ESM format that does import from external library import defaultExport, { namedExport } from 'external-dependency';
  3. webpack build with output.libraryTarget = 'amd' and externals section.

With this combination webpack will produce code that won't work in some cases because it doesn't unwrap "default" export. It also means that if external dependency is also built with webpack it is guaranteed that resulting combination won't work, because webpack produces amd module that returns ESM-like object.

If "type" field is unset, produced output is correct and there is no issue.

For me it looks like a bug, because when I'm writing code in ESM format I'm assuming that (by default) module that I'm importing is ESM too or behaves like ESM. And conversion to amd format should preserve this semantics for externals without depending on type field of package being built.

Link to Minimal Reproduction and step to reproduce

Below are 3 files constituting the project being built
and produced output + profiles for builds with type module set and without it: output.tar.gz

package.json

{
  "name": "main",
  "version": "1.0.0",
  "type": "module",
  "scripts": {
    "build": "webpack"
  },
  "devDependencies": {
    "webpack": "^5.104.1",
    "webpack-cli": "^6.0.1"
  }
 }

index.js

import defaultExport, { namedExport } from 'external-dependency'

console.log(defaultExport, namedExport)

export default defaultExport
export { namedExport }

webpack.config.js

export default {
  entry: "./index.js",
  mode: "none",
  output: {
    libraryTarget: "amd",
  },
  externals: {
    "external-dependency": "amd external-dependency",
  },
}

Expected Behavior

Produced output unpacks default export

Actual Behavior

Produced output doesn't unpack default export

Environment

System:
  OS: macOS 26.2
  CPU: (12) arm64 Apple M2 Max
  Memory: 455.16 MB / 64.00 GB
Binaries:
  Node: 25.5.0 - /opt/homebrew/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 11.8.0 - /opt/homebrew/bin/npm
  pnpm: 10.28.2 - /opt/homebrew/bin/pnpm
Browsers:
  Chrome: 144.0.7559.98
  Edge: 144.0.3719.92
  Firefox: 146.0.1
  Safari: 26.2
Packages:
  webpack: ^5.104.1 => 5.104.1
  webpack-cli: ^6.0.1 => 6.0.1

Is this a regression?

None

Last Working Version

No response

Additional Context

I assume that amd is not the only problematic library target. CommonJS output should have similar issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions