tsx
tsx copied to clipboard
Does not import correctly: .ts file > .mjs package > type: "module" package
Problem
I created a .ts script in my project. It calls multiple dependencies in and outside the project.
One of them is a package (dep-a) whose code is an .mjs file.
This one, in turn, uses the default export from another package (dep-b) that is marked with type: "module", and the code is a .js file.
tsx doesn't treat dep-b correctly as an ES Module and wraps everything in a default export.
/Users/carlos.precioso/Developer/NewWork/tsx-test-case/dep-a/main.mjs:3
export const foo = bar();
^
TypeError: (0 , import_dep_b.default) is not a function
at bar (/Users/carlos.precioso/Developer/NewWork/tsx-test-case/dep-a/main.mjs:3:20)
at Object.<anonymous> (/Users/carlos.precioso/Developer/NewWork/tsx-test-case/dep-a/main.mjs:3:24)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Object.S (/Users/carlos.precioso/Developer/NewWork/tsx-test-case/node_modules/tsx/dist/cjs/index.cjs:1:1250)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:119:18)
at <anonymous> (/Users/carlos.precioso/Developer/NewWork/tsx-test-case/main.ts:1:21)
at Object.<anonymous> (/Users/carlos.precioso/Developer/NewWork/tsx-test-case/main.ts:3:16)
It's worth noting that changing the initial script's extension from
.tsto.mtsor.jsworks fine withtsx. Also changing the initial script from TypeScript to regular JavaScript and changing the extension to.mjsalso works fine with regular Node withouttsx.
Expected behavior
dep-a imports dep-b and uses its default export without any problem.
Minimal reproduction URL
https://stackblitz.com/edit/node-c5szx5?file=main.ts&view=editor
The original project has
dep-aanddep-bas regular npm dependencies innode_modules, but StackBlitz doesn't store the contents, so I moved it to just regular folders with a package.json, because that still shows the error.
Version
v3.13.0
Node.js version
v18.18.0
Package manager
npm
Operating system
macOS
Contributions
- [ ] I plan to open a pull request for this issue
- [ ] I plan to make a financial contribution to this project
Sounds like a duplicate of https://github.com/esbuild-kit/tsx/issues/67
Sounds like a duplicate of #67
I'm not sure, in that issue they are trying to import a CJS file from an ESM file. In my case the problem is in one ESM dependency trying to call another ESM dependency, and failing.
Smaller reproduction: https://stackblitz.com/edit/node-8dqesu?file=index.js
:tada: This issue has been resolved in v4.11.2
If you appreciate this project, please consider supporting this project by sponsoring :heart: :pray: