microsoft / microsoft/TypeScript

Incorrect resolution of CJS types from dual-package dependency in multi-project build with mixed `ESNext/Bundler` and `NodeNext` modules

Open
#57,553 6 comments 1 reaction 1 assignee View on GitHub

@andrewbranch is already working on this.

Since Feb 26, 2024.

Bug Domain: Node ESM
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔎 Search Terms

"NodeNext", "ESNext", "Bundler", "module", "moduleResolution", "ESM", "CJS", "multi-project", "dual package"

🕗 Version & Regression Information
  • This is the behavior in every version I tried (5.0.4, 5.1.6, 5.2.2, 5.3.3, and 5.5.0-dev.20240226), and I reviewed the FAQ for entries about ESM
⏯ Playground Link

https://github.com/haines/typescript-dual-package-esnext-bundler-nodenext

💻 Code

It's probably less confusing to look at the repo, sorry 🙈 I've tried to make it as minimal as possible.

This bug occurs in a multi-project build where one project (app) is configured to use ESNext module and Bundler module resolution, and another (lib) is using NodeNext. Everything is set up to use ESM with "type": "module" in package.json.

If both projects depend on the same dual-packaged dependency (dep), then when app imports dep directly, TypeScript correctly resolves the ESM types. However, transitive imports of dep through lib are resolved to the CJS types.

🙁 Actual behavior

TypeScript resolves the CJS types for transitive imports of the dual-packaged dependency. If the CJS types are not perfectly compatible with the ESM types, then this causes compile errors.

🙂 Expected behavior

TypeScript resolves the ESM types for all imports of the dual-packaged dependency.

Additional information about the issue

Originally encountered in @bufbuild/protobuf: https://github.com/bufbuild/protobuf-es/issues/718

Our app is full-stack and built with Vite, which is why we want to use ESNext/Bundler, but our lib is consumed by other Node.js-only projects that are built with tsc and therefore it makes sense for that to be built with NodeNext.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.