Question around default exports in a dual ESM/CJS package
lukebennett88 opened this issue Β· comments
Sorry if this isn't the right place to raise this, but I'm having some troubles handling default exports in a dual CJS/ESM package.
I'm using tsup in a real project and am seeing this error:
Build tools:
- @arethetypeswrong/cli@^0.12.2
- typescript@^5.2.2
π€¨ CommonJS module simulates a default export with exports.default and exports.__esModule, but does not also set module.exports for compatibility with Node. Node, and some bundlers under certain conditions, do not respect the __esModule marker, so accessing the intended default export will require a .default property access on the default import. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/CJSOnlyExportsDefault.md
βββββββββββββββββββββ¬ββββββββββββββββββββββββ¬ββββββββββββββββββββββββββββββββ¬βββββββββββββββββββββββββββ
β β "package-a" β "package-a/second-entrypoint" β "package-a/package.json" β
βββββββββββββββββββββΌββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β node10 β π€¨ CJS default export β π’ β π’ (JSON) β
βββββββββββββββββββββΌββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β node16 (from CJS) β π€¨ CJS default export β π€¨ CJS default export β π’ (JSON) β
βββββββββββββββββββββΌββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β node16 (from ESM) β π’ (ESM) β π’ (ESM) β π’ (JSON) β
βββββββββββββββββββββΌββββββββββββββββββββββββΌββββββββββββββββββββββββββββββββΌβββββββββββββββββββββββββββ€
β bundler β π’ β π’ β π’ (JSON) β
βββββββββββββββββββββ΄ββββββββββββββββββββββββ΄ββββββββββββββββββββββββββββββββ΄βββββββββββββββββββββββββββ
βELIFECYCLEβ Command failed with exit code 1.
I've put together a reproduction here: https://github.com/lukebennett88/dual-package-example.
My question is β how do I change my code to support this. Am I using tsup
incorrectly? Do I need to change my source code? Is everything configured correctly, do I need a custom esbuild plugin?
Hopefully this isn't too specific to tsup
I'm happy to ask there instead if this is the wrong place π
I think Iβm going to silence this error outside of node16 (from ESM)
, since thatβs the only place it can cause a real issue. Since this is a dual package, real ESM in Node is going to see a real default export, while transpiled ESM will see a transpiled default export, and everything is consistent.