tsx icon indicating copy to clipboard operation
tsx copied to clipboard

Does not import correctly: .ts file > .mjs package > type: "module" package

Open cprecioso opened this issue 2 years ago • 3 comments
trafficstars

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 .ts to .mts or .js works fine with tsx. Also changing the initial script from TypeScript to regular JavaScript and changing the extension to .mjs also works fine with regular Node without tsx.

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-a and dep-b as regular npm dependencies in node_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

cprecioso avatar Oct 06 '23 09:10 cprecioso

Sounds like a duplicate of https://github.com/esbuild-kit/tsx/issues/67

privatenumber avatar Oct 06 '23 19:10 privatenumber

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.

cprecioso avatar Oct 07 '23 16:10 cprecioso

Smaller reproduction: https://stackblitz.com/edit/node-8dqesu?file=index.js

privatenumber avatar Nov 06 '23 07:11 privatenumber

:tada: This issue has been resolved in v4.11.2

If you appreciate this project, please consider supporting this project by sponsoring :heart: :pray:

privatenumber avatar Jun 03 '24 03:06 privatenumber