Types invalid with `"type": "module"` and `"moduleResolution": "Node16"`
See original GitHub issueDescription
Current types/index.d.ts
does not work well when:
- setting
"type": "module"
inpackage.json
. - setting
"moduleResolution": "Node16"
intsconfig.json
’scompilerOptions
.
Reproduction
Issue Analytics
- State:
- Created a year ago
- Comments:7 (6 by maintainers)
Top Results From Across the Web
TypeScript module "Node16" does not resolve types of CJS ...
To expand on this, TypeScript 4.7.1-rc with nodenext module resolution is unable to resolve various NPM dependencies. See this example repo.
Read more >Documentation - Module Resolution - TypeScript
Module resolution is the process the compiler uses to figure out what an import refers to. Consider an import statement like import {...
Read more >Modules: Packages | Node.js v19.3.0 Documentation
A package.json "type" value of "module" tells Node.js to interpret .js ... The .mjs and .cjs extensions can be used to mix types...
Read more >Why doesn't the exports field of npm work in typescript?
Ensure moduleResolution in the consuming library is set to either Node16 or NodeNext in your tsconfig.json file.
Read more >Understanding TypeScript 4.7 and ECMAScript module support
js are loaded as ES modules when the nearest parent package.json file contains a top-level field "type" with a value of "module" ....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Interesting: https://github.com/microsoft/TypeScript/issues/49271#issuecomment-1139688063
So you can fix it by using:
Still I believe TypeScript shouldn’t require the dependency to have
"type":"module"
in its rootpackage.json
for the default import to work, as long asexports.import
is defined inpackage.json
too, pointing to an actual ESM file. Might be worth reporting an issue in TS itself?Edit: reported it here https://github.com/microsoft/TypeScript/issues/50083 but was closed as wontfix
I’ll consider this one fixed, feel free to reopen if I missed anything!