Confused about the `exports` field in package.json? I wrote a guide to break it down and walk you through it! 👉 hirok.io/posts/node-exports-guide
Post
Think it might be worth mentioning in the TS section that types must be first. For example, if you export types + ESM + CJS, the order must be "types, module, default". Also worth maybe mentioning this tool for validation of types + export conditions arethetypeswrong.github.io By @andrewbran.ch
Really good points! I addressed them all - hirok.io/posts/node-e... - hirok.io/posts/node-e... BTW `types` must be nested inside `require` and `import`. Exports with keys types, import, default was the classic mistake everyone made 😵💫
Hmm, I've used "types, import, default" and it seemed to work well? It's also used by eg preconstruct.tools Can you elaborate or point me to some resource on why it's wrong?
Yes, see the blog 😆 The TypeScript docs and 2 validation tools are linked The reason why it’s incorrect is because the d.ts file needs to match the module format of the file its paired with but there’s only one for both ESM and CJS
0 reposts0 likes
Which can be correct with the ESM-wrapper-for-CJS pattern, which is what preconstruct.tools does, and is used in large projects like xstate: arethetypeswrong.github.io?p=xstate%405...
An example I encountered recently is in i18next In v22, they had types, import, require: unpkg.com/i18next@22.5... This had the "🎭 Masquerading as CJS error": arethetypeswrong.github.io?p=i18next%40... And it was fixed in v23: unpkg.com/i18next@23.1... arethetypeswrong.github.io?p=i18next%40...
My understanding is that you only need separate types for esm and CJS if you have a default export. Same types work fine otherwise. Also, here is another preconstruct example without nested types that works: arethetypeswrong.github.io?p=xstate-hel... github.com/VanTanev/xst...
Not sure if that example proves your point: - xstate-helpers doesn't even use exports: unpkg.com/xstate-helpe... - AreTheTypesWrong results show it only resolves to CJS - Package contents show it only has CJS types (no ESM types): unpkg.com/browse/xstat...