Skip to content

ESM/CJS interop: return the named export "module.exports" when require(esm) #20896

@chirsz-ever

Description

@chirsz-ever

Have you used AI?

None

Feature Proposal

This is the mechanism of require(esm) implemented in Node.js1: if the imported module has a named export with the string name "module.exports", then the value corresponding to that export is returned.

In Webpack, the equivalent would be: if an ES Module is imported by require and it has a named export with the string name "module.exports", then the value corresponding to that export should be returned.

Feature Use Case

This makes Webpack compatible with Node.js.

This can help authors convert libraries to ESM-Only, which currently provides dual ESM/CJS modules and depends on using module.exports = ... in CommonJS.

Additional Context

Underscore related issues:

Currently, among various bundlers, I only found rolldown to have implemented this feature2.

Footnotes

  1. https://nodejs.org/docs/latest-v24.x/api/modules.html#loading-ecmascript-modules-using-require

  2. https://github.com/rolldown/rolldown/issues/4997

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Question.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions