I have a Typescript monorepo with source files in <package>/src/*.ts and js files outputted to <package>/dist/*.js. Each package also uses imports and exports in the package.json to control entry points and allow for bare imports. A small repro is linked below.
This configuration works perfectly in Typescript and Node, but not in tsx.
Typescript's language server in VSCode is able to evaluate these imports, highlight suggestions, link correctly to references etc even before anything is compiled
Typescript is able to compile the code
Node.js is able to run the compiled code
However, tsx is NOT able to run this code before it's compiled.
I think the key missing section is here. It looks like we did some extension remapping in https://github.com/privatenumber/tsx/issues/59, but I think we also need handle rootDir and outDir to have full parity with Typescript.
If the package.json is part of the local project, an additional remapping step is performed in order to find the input TypeScript implementation file that will eventually produce the output JavaScript or declaration file path that was resolved from "imports". Without this step, any compilation that resolves an "imports" path would be referencing output files from the previous compilation instead of other input files that are intended to be included in the current compilation. This remapping uses the outDir/declarationDir and rootDir from the tsconfig.json, so using "imports" usually requires an explicit rootDir to be set.
node:internal/modules/cjs/loader:1068
const err = new Error(`Cannot find module '${request}'`);
^
Error: Cannot find module '/Users/philliphuang/ts-repo-structure/node_modules/b/dist/a.js'
at createEsmNotFoundErr (node:internal/modules/cjs/loader:1068:15)
at finalizeEsmResolution (node:internal/modules/cjs/loader:1061:15)
at resolveExports (node:internal/modules/cjs/loader:538:14)
at Module._findPath (node:internal/modules/cjs/loader:607:31)
at Module._resolveFilename (node:internal/modules/cjs/loader:1033:27)
at resolve (/Users/philliphuang/ts-repo-structure/node_modules/tsx/dist/register-BujtrvNV.cjs:1:3084)
at resolveRequest (/Users/philliphuang/ts-repo-structure/node_modules/tsx/dist/register-BujtrvNV.cjs:1:2618)
at Function._resolveFilename (/Users/philliphuang/ts-repo-structure/node_modules/tsx/dist/register-BujtrvNV.cjs:1:3400)
at Module._load (node:internal/modules/cjs/loader:893:27)
at Module.require (node:internal/modules/cjs/loader:1113:19) {
code: 'MODULE_NOT_FOUND',
path: '/Users/philliphuang/ts-repo-structure/node_modules/b/package.json'
}
Bugs are expected to be fixed by those affected by it
[ ] I'm interested in working on this issue
Compensating engineering work will speed up resolution and support the project
[ ] I'm willing to offer $10 for financial support
Acknowledgements
Minimal reproduction URL
https://github.com/JavaScriptBach/ts-repo-structure
Problem & expected behavior (under 200 words)
The Problem
I have a Typescript monorepo with source files in
<package>/src/*.ts
and js files outputted to<package>/dist/*.js
. Each package also usesimports
andexports
in the package.json to control entry points and allow for bare imports. A small repro is linked below.This configuration works perfectly in Typescript and Node, but not in
tsx
.tsx
is NOT able to run this code before it's compiled.I think the key missing section is here. It looks like we did some extension remapping in https://github.com/privatenumber/tsx/issues/59, but I think we also need handle
rootDir
andoutDir
to have full parity with Typescript.Repro
yarn
npx tsx a/src/a.ts
Bugs are expected to be fixed by those affected by it
Compensating engineering work will speed up resolution and support the project