-
Notifications
You must be signed in to change notification settings - Fork 52
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 error in "moduleResolution": "node16" due to "type": "module" in package.json #75
Comments
Thank you for the detailed report, this is very helpful! |
Changing the Another alternative similar to option 2 would be to keep The takeaway is that if the package is supposed to be consumable as CommonJS with type definitions, there needs to be some way for Typescript to import the definitions themselves as CommonJS in order for them to work under The one saving grace here is that, unlike CJS importing ESM, ESM is pretty permissive about importing CommonJS, so as long as the types behave the same in both contexts, you can mostly get away with it. |
Here is another attempt: |
Hi, thanks for the great work on this package. I've run into an issue when trying to import it from a project using Typescript 4.7 with
compilerOptions.moduleResolution
set to"Node16"
and the output module format set to"CommonJS"
. Due to"type": "module"
being set inmsgpackr
'spackage.json
, Typescript treats the main types entry point as being ESM-only, even though based on themain
,module
, andexports
fields, both ESM and CommonJS entries are available. This results in the following error when importing from a CommonJS context:The same issue was brought up in the Typescript repo here with some discussion on ways to approach it: microsoft/TypeScript#49299
From what I understand, there are a couple options:
index.d.mts
andindex.d.cts
files and extend the"exports"
entries to also include"types"
conditions, for example:Unfortunately, due to Typescript's interpretation of
"type": "module"
being so strict, this breaks all of the relative imports insideindex.d.cts
to other*.d.ts
files. Not sure if there's a way around this other than making*.d.cts
copies of all the other definitions too, which is pretty gross."type": "module"
field, and rename all of the*.js
files to*.mjs
. In this case, Typescript will treat the type definitions as CommonJS, but this probably doesn't matter since the major difference between the two modes from Typescript's perspective seems to be how default exports are handled, of whichmsgpackr
has none. This option is less correct, but probably more compatible.Here is a minimal repro:
package.json
tsconfig.json
test.ts
The text was updated successfully, but these errors were encountered: