Skip to content
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

import.meta.url mismatch #448

Open
4 of 6 tasks
laverdet opened this issue Dec 29, 2023 · 0 comments
Open
4 of 6 tasks

import.meta.url mismatch #448

laverdet opened this issue Dec 29, 2023 · 0 comments
Labels
bug Something isn't working pending triage

Comments

@laverdet
Copy link

Acknowledgements

  • I searched existing issues before opening this one to avoid duplicates
  • I understand this is not a place for seek help, but to report a bug
  • I understand that the bug must be proven first with a minimal reproduction
  • I will be polite, respectful, and considerate of people's time and effort

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 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.

Output from the Stackblitz:

> tsc
> tsc -b && node dist/index.js

file:///home/projects/node-dhrkmk/dist/index.js

> tsx
> tsx index.ts

file:///home/projects/node-dhrkmk/index.ts

index.ts:

console.log(import.meta.url);

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 resolved url 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.

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 replace ts-node which handles this case correctly: https://github.com/braidnetworks/loaderkit/blob/c7d8280d5434f4d81c68784a0a1910911048da73/packages/ts/loader.ts#L246-L254

Contributions

  • I plan to open a pull request for this issue
  • I plan to make a financial contribution to this project
@laverdet laverdet added bug Something isn't working pending triage labels Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pending triage
Projects
None yet
Development

No branches or pull requests

1 participant