Report #67903
[bug\_fix] Module 'X' can only be default-imported using the 'esModuleInterop' flag or 'allowSyntheticDefaultImports'
This occurs when importing a CommonJS module \(using module.exports = ...\) as if it were an ES module default export \(import foo from 'foo'\), but esModuleInterop is disabled. CommonJS modules are typed with 'export = ' in TypeScript declarations. Without esModuleInterop, you must use 'import \* as foo from foo' \(namespace import\) or 'import foo = require\(foo\)' \(CommonJS import syntax\). The recommended fix is to enable 'esModuleInterop': true \(and usually 'allowSyntheticDefaultImports': true, which is implied by esModuleInterop\). This allows TypeScript to treat CommonJS default exports as ES module defaults and emits helper code to check the \_\_esModule flag at runtime for interoperability.
Journey Context:
Developer attempts to import a popular npm package like React or lodash using default import syntax: 'import React from react;'. TypeScript immediately flags the error: 'Module react can only be default-imported using the esModuleInterop flag'. The developer checks node\_modules/@types/react and sees it uses 'export = React' syntax, indicating it's a CommonJS-style module. They try changing the import to 'import \* as React from react', which resolves the type error but breaks their JSX configuration or causes React to be typed as the module namespace rather than the React object itself. They investigate the tsconfig options and discover esModuleInterop. Enabling it allows the default import syntax to work. They learn that without this flag, TypeScript treats CommonJS modules strictly according to the ES module spec, where a namespace object is the only valid import target for such modules. The esModuleInterop flag adds a helper that checks if the module has a \_\_esModule property \(indicating it was transpiled from ES modules\) and if so, uses the .default property, otherwise using the module.exports directly. This allows seamless interoperability between CommonJS and ES modules.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T20:27:25.021209+00:00— report_created — created