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

Add --module node22 (or --module node20) with support for require(esm) #60534

Open
6 tasks done
jfirebaugh opened this issue Nov 19, 2024 · 0 comments · May be fixed by #60761
Open
6 tasks done

Add --module node22 (or --module node20) with support for require(esm) #60534

jfirebaugh opened this issue Nov 19, 2024 · 0 comments · May be fixed by #60761
Assignees
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.

Comments

@jfirebaugh
Copy link

🔍 Search Terms

TS1479 is "The current file is a CommonJS module whose imports will produce 'require' calls; however, the referenced file is an ECMAScript module and cannot be imported with 'require'. Consider writing a dynamic import call instead."

But require(ESM) is now supported -- under --experimental-require-module in node 20.x and 22.x and by default in node 23.x -- providing that the module graph contains no top-level await. So TypeScript needs a way to disable TS1479 when a user can assert that this is the case.

✅ Viability Checklist

⭐ Suggestion

"module": "nodenext" should disable TS1479.

📃 Motivating Example

Prototype blog post announcement:

TypeScript now supports require(ESM)

In CJS packages whose tsconfig specifies "module": "nodenext", TypeScript will no longer produce an error when requiring a module known to be ESM.

For example, the following will now work:
module_a/package.json:

{
  ...
  "type": "module"
}

module_a/foo.js:

export const foo = "foo"

module_a/package.json:
(no "type": "module")

module_b/tsconfig.json:

{
  "compilerOptions": {
    "module": "nodenext"
  }
}

module_b/foo.ts:

import foo from "module_a/foo"

💻 Use Cases

  1. What do you want to use this for?
    Interoperability between ESM and CJS.
  2. What shortcomings exist with current approaches?
    TypeScript does not allow CJS to require(ESM), even when it is supported by node.
  3. What workarounds are you using in the meantime?
    Have to avoid doing that.
@jfirebaugh jfirebaugh changed the title Need a way to turn off TS1479 when require(ESM) _is_ supported Need a way to turn off TS1479 when require(ESM) is supported Nov 19, 2024
@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Nov 28, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.8.0 milestone Nov 28, 2024
@andrewbranch andrewbranch changed the title Need a way to turn off TS1479 when require(ESM) is supported Add --module node22 (or --module node20) with support for require(esm) Dec 6, 2024
@typescript-bot typescript-bot added Fix Available A PR has been opened for this issue labels Dec 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fix Available A PR has been opened for this issue Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants