-
-
Notifications
You must be signed in to change notification settings - Fork 160
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
Cannot import named export from a typescript file in a workspace package if entry is using es modules and other package is not. #38
Comments
I investigated this and it might be tough to fix. When a ESM file imports a CommonJS file, you would imagine a loader could simply transform the CommonJS file to ESM syntax on import so it's interoperable. However, Node.js actually bypasses the CJS loader and reads the CommonJS file directly from disk to statically detect named exports with Static analysis is likely necessary because the import statement must assert that the named export exists before running the file. And even if Node.js adds a hook to transform the file before statically analyzing it, we won't be able to support older Node.js versions. I tackled this problem before (evanw/esbuild#2248) but arrived at the same conclusion. However, I managed to fix it for dynamic imports. |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, I don't understand what you're saying. I'm going to hide your comments because they're cryptic and makes this thread harder to follow. In the future, please provide a reproduction when you report a problem. That would be the best way to communicate it, especially if it's not easy to describe it. |
I have just crashed against what seems like the same problem that @loynoir was trying to explain... Essentially, there doesn't seem to be a way to import My situation is that I have a non-module workspace in which I'm creating a typescript server module. This has to have an The modules that I am trying to import are generated from That however doesn't work with To illustrate, here is what I observed /**
* - "a.js" & "a.d.ts" exist
* - results in "tsx" trying to import "a.ts"
*/
import {a} from `../gen/a.js`;
/**
* - same files as before
* - tries and fails to import verbatin (extentionless)
*/
import {a} from `../gen/a`;
/**
* - "a.ts" exists
* - produces following error, probably meaning it does't consider it a module:
* index.mts:1
* import {a} from 'a.js';
* ^
* SyntaxError: The requested module 'a.js' does not provide an export named 'a'
*/
import {a} from '../gen/net_pb.js'; Considering that these are my options of generated code, I'm seemingly left with an only option… generate the code with options |
I have also tried adding What does work is:
With all that said, my context is Yarn V3 workspace with PNP enabled and It might also be worth mentioning that the |
Worth mentioning this bug prevents named imports from Also, this is finally fixable when this lands in Node nodejs/node#50839 |
This comment was marked as resolved.
This comment was marked as resolved.
Workaround for privatenumber/tsx#38 But also, it's time for those to be TypeScript Note, I added `// @ts-nocheck` to the top of the files to suppress TypeScript errors for now, but this should be removed once we move way from Backbone
Currently blocked by nodejs/node#51327 |
This comment has been minimized.
This comment has been minimized.
The issue is acknowledged, and the blocker is referenced above. Contributions to the Node project to fix the issue will be appreciated. To maintain focus, I'm locking this thread until unblocked, but encourage constructive solution-oriented dialogue through PRs. |
Minimal reproduction: https://stackblitz.com/edit/stackblitz-starters-f3qfyj?file=index.mts |
🎉 This issue has been resolved in v4.12.0 If you appreciate this project, please consider supporting this project by sponsoring ❤️ 🙏 |
I managed to fix this without nodejs/node#51327 for Node v20.11+ & v21.3+ CommonJS files loaded via ESM hook doesn't have full CJS support, so the approach I arrived at is actually more compatible. Please try it out and let me know! |
I came here after running into this issue with named exports in lodash and as far as I can tell the issue with using named exports in lodash is an issue with the lodash package and not related to tsx, i.e. it still doesn't work if you just use |
Just installed
I tested with these versions: |
^ same using node-postgres with: |
tsx doesn't add more CJS parsing functionality to Node—it just signals how it could parse exports from TypeScript files. That said, I was able to successfully do named imports from both I'm going to lock this thread because it's being abused as a way to file bug reports without providing reproduction. This often wastes my time because I investigate and it turns out to be working fine. |
Cannot import named export from a typescript file in a workspace package if entry is using es modules and other package is not.
tsx will throw
I know this could be considered a node limitation but maybe we can compile typescript files to esm if entry is esm (even if workspace package does not have
"type": "module"
Repro: https://github.com/remorses/tsx-repros
The text was updated successfully, but these errors were encountered: