You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
tsx forwards the TypeScript source filename as the value of import.meta.url, instead of the simulated JavaScript output filename. This causes an inconsistency at runtime between a tsc-compiled environment and a tsx-loaded environment. The mismatch here will cause complexity when deploying production builds which don't use tsx.
What you can do is set responseURL in the load hook to the name of what the output file would be under tsc. You should leave the resolvedurl to match the source file. In the case noEmit or emitDeclarationOnly is declared in tsconfig then you can leave the TypeScript source filename as-is because in that case you know the project will not be compiled.
Acknowledgements
Minimal reproduction URL
https://stackblitz.com/edit/node-dhrkmk
Version
v4.7.0
Node.js version
v20.8.0
Package manager
npm
Operating system
macOS
Problem & Expected behavior
tsx
forwards the TypeScript source filename as the value ofimport.meta.url
, instead of the simulated JavaScript output filename. This causes an inconsistency at runtime between a tsc-compiled environment and a tsx-loaded environment. The mismatch here will cause complexity when deploying production builds which don't use tsx.Output from the Stackblitz:
index.ts:
What you can do is set
responseURL
in the load hook to the name of what the output file would be under tsc. You should leave the resolvedurl
to match the source file. In the casenoEmit
oremitDeclarationOnly
is declared in tsconfig then you can leave the TypeScript source filename as-is because in that case you know the project will not be compiled.I'm glad to see this project gaining steam because I feel
ts-node
is encouraging a lot of bad practices in the module ecosystem. I think the fix I mentioned here would be really helpful. Our team made a custom loader to replacets-node
which handles this case correctly: https://github.com/braidnetworks/loaderkit/blob/c7d8280d5434f4d81c68784a0a1910911048da73/packages/ts/loader.ts#L246-L254Contributions
The text was updated successfully, but these errors were encountered: