Thoughts around supporting Node.js subpath imports #1799
thetutlage
started this conversation in
General
Replies: 1 comment 5 replies
-
I believe that this issue can be renamed to something like this:
Support resolving from outDir to srcDir
Because that's really what we're talking about. We do support subpath
imports. The problem is that we don't attempt to jump from the outDir back
to the srcDir when resolving modules.
Does this assessment sound accurate to you?
…On Fri, Jun 17, 2022, 4:55 AM Harminder Virk ***@***.***> wrote:
The subpath imports <https://nodejs.org/api/packages.html#imports> are
the aliases one can use with the project to avoid typing relative paths
during imports.
Back story
-
So, instead of typing:
import foo from './src/foo.js'
-
You can define a subpath import alias inside the package.json file
{
"imports": {
"#src/*": "./src/*.js"
}
}
-
And then write imports as follows
import foo from '#src/foo'
The tsc behavior
Now there are a few issues in order to make it work.
-
First, once I compile my TypeScript code to JavaScript, it is written
inside a sub-directory called build and the subpath import alias
becomes invalid. To overcome this issue, I define the import alias to look
inside the build directory instead.
{
"imports": {
"#src/*": "./build/src/*.js"
}
}
-
Luckily with Typescript >= 4.7, I have been able to compile this code
using tsc. Even though the build directory does not exists pre-build,
the TypeScript compiler is smartly able to resolve these imports.
The ts-node behavior
However, now ts-node is not able to find the imports where the alias is
pointing inside the ./build directory (because there is no build
directory).
Now, the main question is, are there any on-going discussions or ideas on
how to make subpath imports work when they are pointing within the build
directory, because tsc is able to handle them.
—
Reply to this email directly, view it on GitHub
<#1799>, or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAC35ODMHPZQM646KT3IP3DVPQ4OXANCNFSM5ZBSOG7Q>
.
You are receiving this because you are subscribed to this thread.Message
ID: ***@***.***>
|
Beta Was this translation helpful? Give feedback.
5 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
The subpath imports are the aliases one can use with the project to avoid typing relative paths during imports.
Back story
So, instead of typing:
You can define a subpath import alias inside the
package.json
fileAnd then write imports as follows
The
tsc
behaviorNow there are a few issues in order to make it work.
First, once I compile my TypeScript code to JavaScript, it is written inside a sub-directory called
build
and the subpath import alias becomes invalid. To overcome this issue, I define the import alias to look inside thebuild
directory instead.Luckily with Typescript >= 4.7, I have been able to compile this code using
tsc
. Even though thebuild
directory does not exists pre-build, the TypeScript compiler is smartly able to resolve these imports.The
ts-node
behaviorHowever, now
ts-node
is not able to find the imports where the alias is pointing inside the./build
directory (because there is no build directory).Now, the main question is, are there any on-going discussions or ideas on how to make subpath imports work when they are pointing within the
build
directory, becausetsc
is able to handle them.I am using
node --loader=ts-node/esm
to execute the fileBeta Was this translation helpful? Give feedback.
All reactions