Skip to content

Conversation

@andrewbranch
Copy link
Member

@andrewbranch andrewbranch commented May 8, 2024

Fixes #54102

Adds a flag --moduleFormatInterop to describe how imports of CommonJS modules and requires of ES modules work in the target runtime/bundler. The values are:

  • node16/nodenext: the current behavior in --module node16/nodenext. That is, a default import of a CommonJS module always binds to module.exports.
  • bundler: the current behavior in all other module modes. Default imports of CommonJS modules bind to either module.exports or module.exports.default, depending on heuristics related to the presence/absence of __esModule.
  • bundlernode: a behavior not achievable without this PR, which is required for good compatibility with Webpack and esbuild. For compatibility reasons, those bundlers use Node.js’s logic for importing CommonJS modules when the importing file would be interpreted as ESM by Node.js due to file extension or package.json "type". I explained this in more detail in a demo repository. This mode toggles the typing of imports based on module format detection, but continues to allow require(esm). (Note this is not a perfect fit for Webpack, which also prohibits require(esm) under the same Node.js compatibility conditions.)

An alternate approach splitting import typing and require(esm) behavior is up at #58526.

@typescript-bot
Copy link
Collaborator

Looks like you're introducing a change to the public API surface area. If this includes breaking changes, please document them on our wiki's API Breaking Changes page.

Also, please make sure @DanielRosenwasser and @RyanCavanaugh are aware of the changes, just as a heads up.

@typescript-bot typescript-bot added Author: Team For Milestone Bug PRs that fix a bug with a specific milestone labels May 8, 2024
@microsoft microsoft locked as resolved and limited conversation to collaborators Oct 16, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Author: Team For Milestone Bug PRs that fix a bug with a specific milestone

Projects

None yet

Development

Successfully merging this pull request may close these issues.

moduleResolution: bundler diverges from bundlers following node semantics in regards to default import

2 participants