-
-
Notifications
You must be signed in to change notification settings - Fork 534
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
Support exports and imports subpath (package.json) pointing to generated .js files #1974
Comments
Ran into this issue myself when working in a multi package repo. Only solution I could find was transpile all packages in watch mode, in which case there's no longer a benefit to using ts-node. Switched to @esbuild-kit/esm-loader and that seems to handle it out of the box. This dividab/tsconfig-paths#243 seems related for my setup, since I do have paths setup between the packages to match their package.json exports. |
Are u developing a package or app? |
@mihaa1 developing multiple packages in a monorepo. There's no issue with the paths I have setup because they only serve to mimic nodes actual behavior. If I wasn't in a monorepo I wouldn't need them, or if typescript had built in support for monorepo setups. |
Desired Behavior
Consider a source file
src/package-a/index.ts
that will be transpiled by tsc tobuild/package-a/index.js
, and apackage.json
that looks like this:Now TypeScript allows me to import
import {...} from #package-a
in any other file. Even when I am importing the generated.js
file (that maybe don't exists yet), TypeScript knows that file will be generated from the.ts
insrc
and correctly type-check that file, uses it for editor support, etc.ts-node
, however, doesn't know how to resolve the file and fails withThe same occurs for subpath
exports
: TypeScript resolves to the source file that will generate that.js
butts-node
doesn't.I would expect
ts-node
to follow TypeScript semantics here and resolve to the corresponding.ts
file as the language server does.Is this request related to a problem?
Using subpath imports (since v14, v12 in experimental) is a common practice to alleviate the burden of nested, relative paths, as well to provide "internal packages" and many other cases. TypeScript, with a well configured
tsconfig.json
andpackage.json
, just works.The issue is, you can't point to the
.ts
file in the"imports"
field of thepackage.json
to makets-node
works since that field will also be read bynode
at runtime to resolve the import, so it must point to the generated.js
. This issue makes it really difficult to correctly use this Node feature.Alternatives you've considered
My first idea was to use conditional imports so the
package.json
could look something like this:However I can't figure out how to use a custom condition for
ts-node
(I'm usingts-node-esm
but don't think that's relevant). I have tested withNODE_OPTIONS="-C ts-node" npx ts-node-esm thing
but got the same error. A possible workaround is to allow custom conditions and the last pattern should work, but I still think the original example should be supported byts-node
out of the box (or at least via some config).Additional context
I think the issue was commented on #1007, but that's a very long thread to follow a possible resolution there.
The text was updated successfully, but these errors were encountered: