Repository navigation
fix: add exports map so bundlers resolve lib/* to the ESM build - #631
Conversation
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
There was a problem hiding this comment.
🟡 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 themodulecondition. - Provides type declaration targets for root and deep imports.
- Explicitly exports
esm/*subpaths andpackage.json.
| File | Description |
|---|---|
package.json |
Adds an exports map controlling bundler, Node, and TypeScript resolution. |
Review effort: Balanced
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.
|
Thanks for the quick review! Pushed 8e9f39d:
One heads-up: with |

Fixes #619
echarts-for-react/lib/core, the import the README recommends for tree-shaking, always loads the CJS build because the package has noexportsfield. Vite 8's consistent CommonJS interop (and esbuild) follow Node's rules in"type": "module"projects, so the default import is the wholemodule.exportsobject rather than the component, and React throws "Element type is invalid … got: object".This adds an
exportsmap. Bundlers, via themodulecondition, get the ESM build for.and./lib/*. Everything else, including Node, gets the same CJS files as today.echarts-for-react,lib,lib/coreandlib/core.jskeep resolving. Per review,esm/*andpackage.jsonare no longer exported.I used
modulerather thanimporton purpose. Theesm/files are ES modules with a.jsextension in a package without"type": "module", so Node can't load them, and animportcondition would break Node and SSR users.Tested by packing the package before and after:
"type": "module"(the issue)lib/coredefault is an object"type": "module"lib/coredefault is an object"type": "module"lib/corefails to resolve (needs an extension)"type": "module"require(), all pathsmoduleResolution: node16lib/coretypesJest passes (13/13). Not tested: Next.js with Turbopack, Parcel, Rspack.
An
exportsmap blocks deep imports it doesn't list. Everything underlib/is covered,esm/andsrc/aren't. Happy to add it, or to update the README, if you'd like.