Report #71999
[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'. Consider writing a dynamic 'import\(\)' call to allow the module to be loaded dynamically.
Convert the importing file to ESM by renaming it to use the \`.mts\` extension \(or \`.mjs\` for compiled output\), or add \`"type": "module"\` to the project's package.json \(ensuring all \`.ts\` files are treated as ESM\), or use a dynamic \`import\(\)\` instead of a static \`import\` statement to load the ESM module from a CJS context. Additionally, ensure \`tsconfig.json\` uses \`"module": "NodeNext"\` and \`"moduleResolution": "NodeNext"\` \(or Node16\) to align TypeScript's resolution with Node.js native ESM rules. The root cause is a mismatch where a file being treated as CommonJS \(due to \`.cts\` extension, lack of \`"type": "module"\`, or \`module: CommonJS\` in tsconfig\) is attempting to statically import an ECMAScript Module \(ESM\), which Node.js forbids in CJS static imports.
Journey Context:
A developer is migrating a Node.js project to ESM. They set \`"module": "NodeNext"\` and \`"moduleResolution": "NodeNext"\` in tsconfig.json and add \`"type": "module"\` to package.json. They write a file \`src/utils.ts\` and try to import an ESM-only package like \`chalk\` \(version 5\+\). They also have an older file \`src/config.cts\` \(CommonJS\) that tries to import from \`src/utils.ts\`. TypeScript throws the error about the CommonJS module producing 'require' calls. The developer is confused because they thought \`module: NodeNext\` would handle everything. They realize that \`.cts\` files force CommonJS mode, and CJS cannot statically import ESM. They consider using \`await import\(\)\` in the \`.cts\` file, but that requires making the function async. Alternatively, they convert the \`.cts\` file to \`.ts\` \(removing the cts extension\) so it becomes ESM via the package.json "type": "module", allowing the static import. They also ensure that relative imports in ESM use \`.js\` extensions \(e.g., \`import \{ x \} from './utils.js'\`\). After renaming and fixing extensions, the error resolves.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T03:25:52.362489+00:00— report_created — created