capricorn86 / happy-dom

A JavaScript implementation of a web browser without its graphical user interface
MIT License
3.08k stars 185 forks source link

Bug: Types for CJS are not exported correctly #1430

Open zirkelc opened 1 month ago

zirkelc commented 1 month ago

Describe the bug The TS types for CJS are not exported correctly, which causes the "module": "node16" and "moduleResolution": "node16" to report errors. See this repo for reproduction: https://github.com/zirkelc/happy-dom-types-node16

Screenshots If applicable, add screenshots to help explain your problem. 2024-05-08_08-59-39

Additional context I have experienced this issue multiple times with different packages. For example, LangChain had the same problem which I described here: https://github.com/langchain-ai/langchainjs/issues/4060#issuecomment-1897863598

When you export CJS files as index.cjs, then you need to export the corresponding type declarations as index.d.cts and not index.d.ts. Here is a short video that demonstrates the fix:

https://github.com/capricorn86/happy-dom/assets/950244/19b010cf-7de6-4876-af16-d3462b716ab5

As you can see, all you need to do is to change the file extension.

Last but not least: thank you for work on this package!