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 Node Package Subpath Pattern Imports #44848
Comments
I would love to support this feature! |
1 similar comment
I would love to support this feature! |
As a workaround, you can use the An example below an alias I use for the root of my project, // package.json
"imports": {
"#@/*": "./*"
}, // tsconfig.json
{
"compilerOptions": {
// ...
"paths": {
"#@/*": ["./*"]
}
}
} |
Thanks for the tip @Cobertos However, this solution is only suitable for application development, when you don't need to export See #26722 |
Relevant link to nodejs "subpath imports" docs: https://nodejs.org/docs/latest-v16.x/api/packages.html#subpath-exports |
If I'm reading things correctly, this actually shipped in TS 4.5 beta (link) as part of "ECMAScript Module Support in Node.js" but finally didn't make it into TS 4.5 stable (link)
Also, not yet in TS 4.6. |
@jakub-g Correct - here are related issues with more context: |
It would appear that such behavior is expected:
Perhaps the problem applies to subpath pattern exports, but that's not what is demonstrated in the details of this issue. |
Another issue faced is the path during development and post build are not compatible. Given the following subpath imports. "imports": {
"#src/*": "./src/*"
} If I compile my build with the Not sure, if TypeScript can fix this behavior, but it is going to be a common theme, where the subpath imports need to look inside a separate directory post build. |
@weswigham let's try to make a call on this one based on what we know now |
? We shipped export map support in 4.7, complete with pattern exports? Not sure there's anything else to do? |
If the answer is "we did it and it's done" that's perfect 👍 |
Isn't this issue talking about subpath imports? |
We should support those equally as much - point them at your output file paths, since that's what you need at runtime in node to make the paths work in the built js. |
But that makes the editor complain during development time since there is no output file. Also, will be nice to see all this getting documented on the official website :) |
Has anyone gotten this to work? Using latest typescript (4.7.4), I still am seeing the problem where subpath imports and tsconfig paths don't work together. Repro is https://github.com/doronrosenberg/ts-subpath-imports, you can run tsc in the project and see that it can't resolve the imports from the subpath imports. Or am I doing something wrong? |
@doronrosenberg Subpath imports needs a Other than the tsconfig, you have to fix the package.json: Also, in I have sent a PR over at doronrosenberg/ts-subpath-imports#1. As for the tsconfig |
This definitely doesn't work / isn't done. Say, for example, you have this in your import * from '#/components" ...and this in your "compilerOptions": {
// ...
"paths": {
"#/*": [
"src/*"
]
}
} And you export to "imports": {
"#/*": "dist/module/*"
}, However, the exported That said, the |
I'm struggling to get this feature implemented and it seems like there is mass confusion around the core naming. Subpath exports work fine. Subpath imports appear to be non-functional for most use-cases. The core issue I'm having is that when I'm working within my own library I can safely do something like Heres the relevant part of my package.json:
I believe the way to handle this condition is that we need TS to specify a community "condition" via the
I've tried a ton of different permutations and I just can't see to get them to work, some step of the toolchain always falls apart. If I do this in raw JS it works fine. Edit: I was able to figure out a semi-workaround for this, using the following mechanisms:
So now when I run via |
I made a demo about how to make this pattern a bit less complex https://github.com/bmeck/typescript-issue-44848-demo While this works at runtime, it seems there is an outstanding bug per #57259 |
Bug Report
Typescript ignores Package Subpath Imports breaking the linter when using a loader. These "aliases" are not optional when you have multiple entry points or executables at different levels.
Related to #33079
Making a new bug as the conversation there is about exports... but the import side is also important and has a seperate set of solutions.
🔎 Search Terms
Module alias, import patterns, pwd, es modules, node
🕗 Version & Regression Information
Please keep and fill in the line that best applies:
⏯ Playground Link
Running node 16 with ts-node loader in a native ES Module package.
Coming soon from runkit. Integration issues like this are tricky. But I've got the tools to make it so.
💻 Code
🙁 Actual behavior
"Cannot find module '#src/models/mongodb.js' or its corresponding type declarations.ts(2307)"
🙂 Expected behavior
Should work like regular javascript.
The text was updated successfully, but these errors were encountered: