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
"require" condition not respected when imported package has "type": "module" #49391
Comments
If |
In short, your idea of using a |
Correction: It removes the TypeScript warning mentioned in the "Actual behavior" section. |
I can confirm your solution also removed the TypeScript warning. ".": {
"require": {
"types": "./dist/index.d.cts",
"default": "./dist/index.cjs"
},
"types": "./dist/index.d.ts",
"import": "./dist/index.js"
}, I had tried only a |
This issue has been marked as 'External' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
This comment was marked as resolved.
This comment was marked as resolved.
It seems I was wrong. I tried to reproduce the minimal example here and I could not. It may be that I had an additional |
Bug Report
vitest
package uses"type": "module"
"moduleResolution": "node16"
in its tsconfig"type"
field in package.json, so it defaults tocommonjs
Currently, Vitest doesn't have a
require
condition in itsexports
field, but even if it did, TypeScript 4.7 still assumes the package is ESM only. This leads to TypeScript thinking thatvitest
cannot be imported by test files, as the test files are assumed to be CJS modules, but Vitest actually compiles them as ESM.I have a PR open (vitest-dev/vitest#1411) that removes
"type": "module"
and uses.mjs
extensions instead, and this avoids the issue. But the Vitest team thinks this is something that TypeScript should fix. I believe this is an edge case and so Vitest should fix it, but I'm writing this issue to get the TypeScript team's opinion on the matter.🔎 Search Terms
🙁 Actual behavior
When a test file imports
vitest
, it gets this error:Even if I add a
require
condition the exports map of Vitest.🙂 Expected behavior
Vitest should be able to use a
require
condition to signal to TypeScript that CJS modules are allowed to import thevitest
package without receiving an error message.The text was updated successfully, but these errors were encountered: