Skip to content
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

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

Open
2 tasks
cprecioso opened this issue Oct 6, 2023 · 3 comments
Open
2 tasks
Labels
bug Something isn't working interop

Comments

@cprecioso
Copy link

cprecioso commented Oct 6, 2023

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 cprecioso added bug Something isn't working pending triage labels Oct 6, 2023
@privatenumber
Copy link
Owner

Sounds like a duplicate of #67

@cprecioso
Copy link
Author

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.

@privatenumber
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working interop
Projects
None yet
Development

No branches or pull requests

2 participants