Skip to content

require(esm) 'module.exports' interop export name support #61645

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

Open
6 tasks done
sapphi-red opened this issue May 2, 2025 · 0 comments
Open
6 tasks done

require(esm) 'module.exports' interop export name support #61645

sapphi-red opened this issue May 2, 2025 · 0 comments
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@sapphi-red
Copy link

sapphi-red commented May 2, 2025

🔍 Search Terms

require(esm), module.exports export, node 22.12

✅ Viability Checklist

⭐ Suggestion

Node 20.19.0, 22.12.0, 23.0.0 added requiring ESM files (known as require(esm)). This feature includes module.exports interop export name feature that allows users to customize what the module.exports value would be when a module is required.
(nodejs/node#53848)

Currently TypeScript does not use the module.exports export type when requiring an ESM file.
I'd like TypeScript to use the module.exports export type for the types when that ESM file is required, aligning the type with the actual runtime object.

📃 Motivating Example

I made a reproduction: https://github.com/sapphi-red-repros/typescript-require-esm-module-exports-export
You can see how the types and the actual runtime values are different by the following steps

  1. Run pnpm i
  2. (Run pnpm build) (the built file is already included in the repo)
  3. Run node index.cjs
  4. See types in index.cts

The concrete differences are:

  • testPkg['module.exports' is undefined at runtime, but the type is { foo: string; default: { bar: string; }; bar: string; }
  • testPkg contains bar: string at runtime, but the type does not include that

nodejs/TSC#1622 (comment) describes the motivation of this module.exports export interop feature.

💻 Use Cases

  1. What do you want to use this for?
    To migrate a package that had CJS files in past, to ESM-only while keeping the interface as-is without generating a separate type definition file.

  2. What shortcomings exist with current approaches?
    It requires two type definition types, one for require and one for import, so two builds are needed. The package.json would also be convoluted. main field cannot be used as the type definition file is different for require and import. The exports field would need to be like:

{
  "exports": {
    ".": {
      "types": { "require": "./index.d.cts" },
      "default": "./index.js"
    }
  }
}
  1. What workarounds are you using in the meantime?
    I haven't considered it yet. Probably I'll use the exports field workaround described above.
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label May 2, 2025
@andrewbranch andrewbranch added this to the TypeScript 5.9.0 milestone May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

No branches or pull requests

3 participants