Open jfirebaugh opened 1 week ago
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.
require(ESM)
--experimental-require-module
"module": "nodenext" should disable TS1479.
"module": "nodenext"
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"
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:
module_a/package.json
{ ... "type": "module" }
module_a/foo.js:
module_a/foo.js
export const foo = "foo"
module_a/package.json: (no "type": "module")
"type": "module"
module_b/tsconfig.json:
module_b/tsconfig.json
{ "compilerOptions": { "module": "nodenext" } }
module_b/foo.ts:
module_b/foo.ts
import foo from "module_a/foo"
🔍 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:
💻 Use Cases