Skip to content

fix: add exports map so bundlers resolve lib/* to the ESM build - #631

Merged
hustcc merged 2 commits into
hustcc:masterfrom
Sam-Razavi:fix/exports-map-esm-subpaths
Nov 8, 2026
Merged

hustcc merged 2 commits into
hustcc:masterfrom
Sam-Razavi:fix/exports-map-esm-subpaths

Conversation

@Sam-Razavi

@Sam-Razavi Sam-Razavi commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #619

echarts-for-react/lib/core, the import the README recommends for tree-shaking, always loads the CJS build because the package has no exports field. Vite 8's consistent CommonJS interop (and esbuild) follow Node's rules in "type": "module" projects, so the default import is the whole module.exports object rather than the component, and React throws "Element type is invalid … got: object".

This adds an exports map. Bundlers, via the module condition, get the ESM build for . and ./lib/*. Everything else, including Node, gets the same CJS files as today. echarts-for-react, lib, lib/core and lib/core.js keep resolving. Per review, esm/* and package.json are no longer exported.

I used module rather than import on purpose. The esm/ files are ES modules with a .js extension in a package without "type": "module", so Node can't load them, and an import condition would break Node and SSR users.

Tested by packing the package before and after:

before after
Vite 8 dev, "type": "module" (the issue) crash, lib/core default is an object renders
Vite 8 build / esbuild, "type": "module" lib/core default is an object function
webpack 5, "type": "module" lib/core fails to resolve (needs an extension) resolves
Vite 7 / Vite 8 / webpack / esbuild without "type": "module" works unchanged
Node 22 require(), all paths works unchanged
TypeScript moduleResolution: node16 can't find lib/core types resolves

Jest passes (13/13). Not tested: Next.js with Turbopack, Parcel, Rspack.

An exports map blocks deep imports it doesn't list. Everything under lib/ is covered, esm/ and src/ aren't. Happy to add it, or to update the README, if you'd like.

Deep imports such as echarts-for-react/lib/core always loaded the CJS
build. Under Vite 8's consistent CommonJS interop (and esbuild) in
"type": "module" projects, the default import is then the whole
module object, and React throws "Element type is invalid ... got:
object".

The exports map sends bundlers (the "module" condition) to esm/ for
the main entry and lib/* subpaths, while Node keeps loading the CJS
files it loads today. Existing import paths keep working.

Fixes hustcc#619
@coveralls

coveralls commented Nov 7, 2026 •

Copy link
Copy Markdown

Coverage Status

coverage: 94.737%. remained the same — Sam-Razavi:fix/exports-map-esm-subpaths into hustcc:master

Comment thread package.json Resolved
Comment thread package.json Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The exports wildcard omits echarts-for-react/lib, breaking the existing CJS entry point with ERR_PACKAGE_PATH_NOT_EXPORTED; an explicit ./lib export is needed to preserve compatibility.

1 open issue
What changed in this PR

Adds conditional package exports to address #619 by directing bundlers to the ESM build for lib/* imports while retaining CJS targets for Node.

Changes:

  • Routes the root entry and lib/* subpaths to ESM under the module condition.
  • Provides type declaration targets for root and deep imports.
  • Explicitly exports esm/* subpaths and package.json.
File Description
package.json Adds an exports map controlling bundler, Node, and TypeScript resolution.

Review effort: Balanced

Comment thread package.json Resolved
Without an explicit ./lib entry, require('echarts-for-react/lib') failed
with ERR_PACKAGE_PATH_NOT_EXPORTED. Remove the esm/* and package.json
exports as requested in review.
@Sam-Razavi

Copy link
Copy Markdown
Contributor Author

Thanks for the quick review! Pushed 8e9f39d:

  • Removed the two esm entries and the package.json export.
  • Added an explicit ./lib entry. Copilot's point was right: with only ./lib/*, require('echarts-for-react/lib') failed with ERR_PACKAGE_PATH_NOT_EXPORTED. It now resolves like before.

One heads-up: with esm no longer exported, imports such as echarts-for-react/esm/core (the workaround suggested in #619, also used in #522) fail to resolve in Vite, webpack and esbuild, and require('echarts-for-react/package.json') fails in Node. echarts-for-react/lib/core now gives bundlers the same ESM build, so those users can switch, but it may be worth a line in the release notes. Happy to add either back if you'd prefer.

@hustcc
hustcc requested a balanced review from Copilot November 8, 2026 01:54

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the conditional package exports change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

@hustcc
hustcc merged commit a0e9565 into hustcc:master Nov 8, 2026
3 checks passed
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.

[Bug] Inconsistent CJS interop

4 participants