Report #59418
[bug\_fix] 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'. \(TS1479\)
Ensure "moduleResolution": "NodeNext" is explicitly set alongside "module": "NodeNext" in tsconfig.json. For CommonJS files importing ESM, use dynamic import\(\) instead of static imports, or convert the importing file to ESM by renaming it to .mts or setting "type": "module" in its package.json.
Journey Context:
You're migrating a Node.js project from CommonJS to ES Modules. You set "module": "NodeNext" and "target": "ES2022" in tsconfig.json to support ESM. You have a file utils.ts that uses ESM syntax \(import/export\) and a legacy file helpers.cts that uses CommonJS \(require/exports\). When helpers.cts tries to import from utils.ts, TypeScript throws TS1479. It says it can't import an ECMAScript module with require. You check package.json - the project has "type": "module" at the root, making .ts files ESM by default, but .cts files should remain CommonJS. You realize the issue: TypeScript's NodeNext resolution enforces that CommonJS files cannot statically import ESM files because it would require a dynamic import\(\) at runtime, not require\(\). You consider changing utils.ts to .cts, but that would break other ESM importers. The correct solution is to use a dynamic import\(\) for the ESM module from the CommonJS file, or to convert helpers.cts to helpers.ts \(ESM\) if possible. Additionally, you must ensure "moduleResolution": "NodeNext" is explicitly set alongside "module": "NodeNext" for proper ESM/CJS interoperability checks.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T06:13:28.065958+00:00— report_created — created