-
Notifications
You must be signed in to change notification settings - Fork 141
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
Typescript with module resolution "NodeNext" + type: "commonjs" fails to import the commonJS module #255
Comments
I've implemented a PR in #256. |
Hi @ItamarGronich , thanks a lot for opening this issue and for sending a pull request. Allow me the time to reproduce the bug and fully understand the implications (I will be quite busy in the next few days, and I admit that |
@ItamarGronich I followed the steps for reproduction (thanks a lot for providing them, very helpful 👍 ). I indeed get an error when running
This error, though, seems more related to the reproduction file Is that the same error you are getting? |
Hi @lucaong, yes that's the same error. However, I think this should work in all casses:
So this error shouldn't happen as So I think this error means that for some reason I think something is wrong with the way types are defined in The expected behavior (the way i understand it) is like so:1. Source code
|
Sadly, also i think that my PR doesn't solve the problem - although i thought it did. |
@ItamarGronich I believe the issue is that when This only affects the application files (the
At least this is my understanding of the issue, but feel free to comment further if you think there is actually an issue with MiniSearch. |
I think that with tsc you can always use What's changed with the
|
Uhm you are right, this is more complex than I hoped... Relevant documentation: |
Yeah it's more complicated. Actually, i was doing a bit of research and i found this really cute package: https://www.npmjs.com/package/@arethetypeswrong/cli I was skeptical of how it would work at first but wow it's awesome. itamar.gronich minisearch $ attw --pack .
minisearch v6.3.0
Build tools:
- typescript@^5.2.2
- rollup@^4.1.0
- @rollup/plugin-typescript@^11.0.0
❗️ The resolved types use export default where the JavaScript file appears to use module.exports =. This will cause TypeScript under the node16 module mode to think an extra .default property access is required, but that will likely fail at runtime. These types should use export = instead of export default. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseExportDefault.md
👺 Import resolved to an ESM type declaration file, but a CommonJS JavaScript file. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/FalseESM.md
💀 Import failed to resolve to type declarations or JavaScript files. https://github.com/arethetypeswrong/arethetypeswrong.github.io/blob/main/docs/problems/NoResolution.md
┌───────────────────┬──────────────────────────────┬────────────────────────────┐
│ │ "minisearch" │ "minisearch/SearchableMap" │
├───────────────────┼──────────────────────────────┼────────────────────────────┤
│ node10 │ ❗️ Incorrect default export │ 💀 Resolution failed │
├───────────────────┼──────────────────────────────┼────────────────────────────┤
│ node16 (from CJS) │ 👺 Masquerading as ESM │ 👺 Masquerading as ESM │
├───────────────────┼──────────────────────────────┼────────────────────────────┤
│ node16 (from ESM) │ 🟢 (ESM) │ 🟢 (ESM) │
├───────────────────┼──────────────────────────────┼────────────────────────────┤
│ bundler │ 🟢 │ 🟢 │
└───────────────────┴──────────────────────────────┴────────────────────────────┘ So this read was actually really helpful and i think i know how to solve this now. |
This seems to be a solution: #258 It seems that the actual content of the declaration file does not matter, but its extension does: a Could you please try it out too, and possibly test out with the package you found? To test it out, build it with |
Well the package doesn't like it.
the reason is that This is because it's generated by a third party dts tool and not tsc. |
Unfortunately, I cannot seem to get the I wonder if Rollup is even needed for this. Maybe it could be used only for the UMD bundle, while the ES and CJS versions could be created with |
Why do you need a single dts file? why can't it be many?
Yeah thought about it too. You don't necessarily have to bundle when building for node/bundlers. |
Description
When using with
packageJson.type = 'commonjs'
(the default) and typescript with the following config:tsc
fails to load the commonjs module.After some research apparently tsc loads the
./dist/types/index.d.ts
file from thepackageJson.exports.type
which makes typescript thing that the imported module is a esmodule and not a commonjs module.That's because that in the
index.d.ts
file the way the code is exported is in esm formatExpected behavior
tsc
gets types that are applicable for both esm and commonjs.Reproduction:
The text was updated successfully, but these errors were encountered: