Skip to content

mismatch between type: "module" and UMD exports.browser mapping #38

@ro0gr

Description

@ro0gr

When importing jquery-param using ES Module default import syntax (e.g. import param from 'jquery-param'), the resolved import is undefined in environments that prioritize the "browser" package resolution condition (such as Webpack 5, Rollup, or ESM loaders resolving the browser target).

The last Working Version is 1.2.4 (before "type": "module" and "exports" mapping were introduced).

Root Cause

In package.json, "type": "module" is configured, which mandates that ES Module loaders treat all .js files in the package as ES Modules. However, the "browser" key in the "exports" map resolves to ./jquery-param.min.js, which is compiled as a UMD (CommonJS/AMD/Global) module and contains no
ES export statements:

"exports": {
  ".": {
    "import": "./dist/esm/jquery-param.mjs",
    "require": "./dist/umd/jquery-param.js",
    "browser": "./jquery-param.min.js"
  }
}

When a browser-targeted resolver resolves the "browser" key and parses ./jquery-param.min.js as an ES Module, it finds no export default statement. The module resolves to an empty namespace object ([Module: null prototype] {}), resulting in default being undefined.

Solution / Recommendation

Either:

  1. Update the "browser" export target to point to an ESM bundle (e.g. ./dist/esm/jquery-param.js or a minified ESM version of the script).
  2. Or change the extension of jquery-param.min.js to .cjs (and update the "browser" configuration key accordingly) so that Webpack and Node.js loaders
    recognize it as a CommonJS module and apply default import interop rules correctly.

Reproduction

A minimal reproduction repository running on pure Node.js (with zero bundler overhead) is available here: https://github.com/ro0gr/jquery-param-esm-repro

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions