privatenumber / tsx

⚡️ TypeScript Execute | The easiest way to run TypeScript in Node.js
https://tsx.is
MIT License
9.64k stars 154 forks source link

Import discrepancy in Node's ESM/CJS interop #657

Open steelbrain opened 1 month ago

steelbrain commented 1 month ago

Acknowledgements

Minimal reproduction URL

https://github.com/steelbrain/2024-09-tsx-bug-repro

Problem & expected behavior (under 200 words)

Hey Team! Really confused by the output of tsx changing based on package.json#type instead of tsconfig.json. Please have a look at the repro repository. It's possible that this isn't a bug in tsx and is a behavior coming in from esbuild.

Either way, would love to hear based on your experience why this is happening and any pointers on how we could mitigate the impact. I burned through some solid time trying to isolate the bug that led me here. Happy to do a PR (if someone can walk me through it) or financially sponsor a fix here.

Bugs are expected to be fixed by those affected by it

Compensating engineering work will speed up resolution and support the project

privatenumber commented 1 month ago

This is unexpected behavior but unrelated to tsx. You can run it in Node and get the same behavior. I hope to fix it via tsx in the future though.

Modules can parse named exports from CommonJS via cjs-module-lexer, but that's unable to parse UMD format.

steelbrain commented 1 month ago

Thank you for the pointers @privatenumber. Much appreciated!

I hope to fix it via tsx in the future though.

That would be really helpful. I ran into this while trying to run some code that worked fine in Next.js but broke in tsx. Handling it here would make it easier for people to run tsx on existing codebases.