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

TypeScript resolution not working from JS files (even with allowJs) #135

Closed
1 task
aredridel opened this issue Nov 3, 2022 · 8 comments · Fixed by #535
Closed
1 task

TypeScript resolution not working from JS files (even with allowJs) #135

aredridel opened this issue Nov 3, 2022 · 8 comments · Fixed by #535
Labels

Comments

@aredridel
Copy link

aredridel commented Nov 3, 2022

Bug description

tsx index.mts to run

index.mts imports file.mjs by import x from "./file.mjs"

file.mjs imports rootfile.mts by import x from "./rootfile.mjs"

I expect this to resolve rootfile.mts and work.

❯ tsx index.mts
Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/projects/rootfile.mjs' imported from /home/projects/node-war8wy/file.mjs
    at get (https://node-war8wy.w-corp.staticblitz.com/blitz.a0ea75dd36396805bd36e49d0b54d86602583e15.js:6:292488)
    at U@file:///home/projects/node-war8wy/node_modules/ (esbuild-kit/esm-loader/dist/index.js:19:1634)
    at <anonymous> (<anonymous>) {
  code: 'ERR_MODULE_NOT_FOUND'
}

Reproduction

https://stackblitz.com/edit/node-war8wy has the reproduction

Environment

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 16.14.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 7.17.0 - /usr/local/bin/npm
  npmPackages:
    tsx: ^3.11.0 => 3.11.0 

Can you contribute a fix?

  • I’m interested in opening a pull request for this issue.
@aredridel aredridel added bug Something isn't working pending triage labels Nov 3, 2022
@privatenumber
Copy link
Owner

Thanks for the reproduction.

The .mjs -> .mts resolution isn't happening because it's not coming from a TypeScript file.

Do you know if TypeScript supports that?

BTW In ./file.mjs, it should be importing from ./rootfile.mjs instead of ../rootfile.mjs.

@aredridel
Copy link
Author

BTW In ./file.mjs, it should be importing from ./rootfile.mjs instead of ../rootfile.mjs.

Doh, missed that in my simplification.

@aredridel
Copy link
Author

Do you know if TypeScript supports that?

ts-node does, and tsc does this:

tsc --noEmit --allowJs file.mjs index.mts rootfile.mts

(no output)

but if I rename rootfile.mts to rootfile.bad,

❯ tsc --noEmit --allowJs file.mjs index.mts rootfile.mts
error TS6053: File 'rootfile.mts' not found.
  The file is in the program because:
    Root file specified for compilation


Found 1 error.

@cbetori

This comment was marked as off-topic.

@privatenumber

This comment was marked as off-topic.

@AurelienRibon

This comment was marked as off-topic.

@privatenumber privatenumber changed the title ERR_MODULE_NOT_FOUND importing mts from mjs file from mts file TypeScript resolution not working from JS files (even with allowJs) Mar 7, 2024
@privatenumber
Copy link
Owner

Happy to accept a PR to address this. Basically, tsx should read the allowJs config, and if it's enabled, treat JS files as TypeScript files during resolution.

Locking thread to direct further dialogue in the form of PRs.

Repository owner locked and limited conversation to collaborators Mar 7, 2024
@privatenumber
Copy link
Owner

🎉 This issue has been resolved in v4.7.3

If you appreciate this project, please consider supporting this project by sponsoring ❤️ 🙏

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants