Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with ESM and CJS types exports #336

Closed
fir1 opened this issue Dec 3, 2024 · 1 comment
Closed

Issue with ESM and CJS types exports #336

fir1 opened this issue Dec 3, 2024 · 1 comment

Comments

@fir1
Copy link

fir1 commented Dec 3, 2024

package.json - has issues regarding ESM and CJS support.

This problem usually happens when a library that includes both a CJS and ESM implementation attempts to use a single .d.ts types file to represent both, where the package.json has "type": "module", most often in a structure like:

{
  "name": "pkg",
  "type": "module",
  "exports": {
    ".": {
      "types": "./index.d.ts",
      "import": "./index.js",
      "require": "./index.cjs"
    }
  }
}

If they use import or export at the top level—they must represent exactly one JavaScript file. They especially cannot represent JavaScript files of two different module formats. The example above needs to add a .d.cts file to represent the .cjs file, at which point the package.json can be rewritten as:

{
"name": "pkg",
"type": "module",
"exports": {
".": {
"import": {
"types": "./index.d.ts",
"default": "./index.js"
},
"require": {
"types": "./index.d.cts",
"default": "./index.cjs"
}
}
}
}

The qrcode package is using rollup to build both CJS and EMS modules, there is a known issue with rollup regarding this and possible workaround:
rollup/plugins#1782

Reference to cause and the potential fix:

https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md

@nuintun
Copy link
Owner

nuintun commented Dec 18, 2024

Fixed in v4.1.9

image

@nuintun nuintun closed this as completed Dec 18, 2024
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

No branches or pull requests

2 participants