Skip to content

fix: resolve the ESM build through the default export condition - #7132

Merged
overbalance merged 6 commits into
open-telemetry:mainfrom
embrace-io:overbalance/exports-default-condition
Sep 29, 2026
Merged

overbalance merged 6 commits into
open-telemetry:mainfrom
embrace-io:overbalance/exports-default-condition

Conversation

@overbalance

@overbalance overbalance commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Every exports entry now lists require first and puts the ESM build under default, instead of under import:

".": {
  "require": { "types": "./dist/index.d.cts", "default": "./dist/index.cjs" },
  "default": { "types": "./dist/index.d.mts", "default": "./dist/index.mjs" }
}

With only import and require, the map had no fallback. A resolver that activates neither condition (for example, a custom conditionNames list) failed with "No exports main defined". The Node.js docs recommend ending every condition object with default, and this change adds that fallback.

This split out of the #platform work, which uses the same require/default shape for the imports field.

Resolution changes

  • Resolvers that pick import or require from the import syntax (Node.js, TypeScript, webpack 5, esbuild, Rollup, Parcel, Vite, Jest and Metro 0.84+) resolve the same files as before.
  • Resolvers that activate neither condition now get the ESM build. Before, they failed to resolve.
  • Resolvers that activate both conditions for every request now get the CJS build instead of ESM, because require comes first. One example is Metro before it started to assert only one condition per request, since its default was ['require', 'import'].

How Has This Been Tested?

  • npm run compile
  • npm run verify-pack: all 38 packages resolve. On npm 12 this needs the pack --json fix, which has its own PR.
    • It now also resolves each exports subpath with neither import nor require active, and fails if nothing resolves or the result differs from the import target.
  • npm run test:bundle: all 9 bundler tests pass (webpack 5, Next.js 15/16 edge, Jest jsdom, TypeScript legacy resolution, Metro, Expo).

@overbalance
overbalance requested review from a team as code owners September 29, 2026 00:04
@opentelemetry-pr-dashboard

opentelemetry-pr-dashboard Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Pull request dashboard status

Merged · refreshed 2026-09-29 17:41 UTC

Status above doesn't look right?
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

@codecov

codecov Bot commented Sep 29, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.04%. Comparing base (f443abe) to head (f81c549).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #7132   +/-   ##
=======================================
  Coverage   95.04%   95.04%           
=======================================
  Files         383      383           
  Lines       13468    13468           
  Branches     3100     3100           
=======================================
  Hits        12801    12801           
  Misses        667      667           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@JacksonWeber

Copy link
Copy Markdown
Contributor

Could we add a test where neither import nor require is set, to make sure the ESM fallback works? Not a blocker, but it’d help catch regressions.

@overbalance

Copy link
Copy Markdown
Contributor Author

@JacksonWeber Added in 5801037: verify-pack now resolves every exports subpath with neither import nor require active, and fails if nothing resolves or the result differs from the import target. Confirmed it catches both by temporarily breaking api/package.json.

Comment thread scripts/verify-pack.mjs Outdated
}
}
if (pkg.exports && typeof pkg.exports === 'object') {
for (const [subpath, cond] of Object.entries(pkg.exports)) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: IIUC, this won't handle an "exports" form like:

  "exports": {
    "import": "./index-module.js",
    "require": "./index-require.cjs"
  },

but we don't have any of those and don't expect to, so this is a non-block nit.
Could throw in an assert that blows up if that surprise shows up, but I'm fine either way.

Suggested change
for (const [subpath, cond] of Object.entries(pkg.exports)) {
for (const [subpath, cond] of Object.entries(pkg.exports)) {
assert(subpath.startsWith('.'), `${label} :: exports "${subpath}" isn't a subpath, this "exports" form isn't supported by this script"`);

Would need the import of assert at the top, then.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Rather than assert, I normalized the shorthand the way Node does ({ ".": exports }) in a small subpathMap helper. collectEntries had the same assumption, so both now use it. Your example now fails with has no fallback without import/require. Fixed in f81c549.

@overbalance
overbalance added this pull request to the merge queue Sep 29, 2026
Merged via the queue into open-telemetry:main with commit 87c153c Sep 29, 2026
26 checks passed
@overbalance
overbalance deleted the overbalance/exports-default-condition branch September 29, 2026 17:39
wolfgangcodes added a commit to wolfgangcodes/opentelemetry-js that referenced this pull request Oct 12, 2026
…fault condition

The exports map now uses the "require" and "default" conditions, as in open-telemetry#7132.
The package.json file also drops the "module" field, as in open-telemetry#7130.

Assisted-by: Claude Opus 5.5
Signed-off-by: Wolfgang Therrien <wolfgang@wolfgangcodes.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants