esModuleInterop
esModuleInterop changes how TypeScript types and emits default imports from CommonJS modules. With it on, import fs from "fs" matches the way many runtimes actually expose module.exports.
Detailed Explanation
ES modules have a real default export (export default). CommonJS has module.exports = … and exports.foo = …. These are not the same shape. TypeScript originally modeled CJS imports as import * as fs from "fs" and treated import fs from "fs" as a type error unless the module had an export default.
Real Node and many bundlers, though, let you write import fs from "fs" and give you module.exports. esModuleInterop makes the type checker agree with that, and it changes emit so the compiled JavaScript uses a helper (__importDefault) that picks module.exports when default is missing.
What you get with the flag on:
import x from "cjs-pkg"is allowed when the package is typed asexport =(the CJS default).import * as ns from "cjs-pkg"is a real namespace and may not be callable, matching ESM. The old, looserallowSyntheticDefaultImports-only behavior is not enough on its own for emit.allowSyntheticDefaultImportsis turned on as well, which is the type-side half: default imports are allowed even when emit ispreserve.
What you get with the flag off (the historical default): default-importing a CJS module is an error, and import * as fs from "fs"; fs.existsSync is the documented form. Some older @types packages were written for that world.
The flag does not convert a package to ESM. It does not change package.json "type". It does not fix a package whose types say export default when the JS is exports.foo. It only aligns TypeScript's default-import story with the CJS interop most people already use.
Under module: "NodeNext", resolution and emit follow Node more closely, and esModuleInterop is still recommended so default imports of CJS packages type-check. Bundler projects almost always want it on. New tsc --init configs set it to true.
The helper it injects is small. If you emit to ESM for a runtime that already performs this interop, you may prefer module: "nodenext" plus the runtime's own rules, and still keep the flag for types. If you see __importDefault in output and you thought you were emitting "pure ESM," look at this flag and at importHelpers / tslib.
Code Examples
1. Turn it on next to a modern module setting.
// 1. Turn it on next to a modern module setting.
{
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"strict": true
}
}2. Default-importing a CommonJS module (types use export =).
// 2. Default-importing a CommonJS module (types use export =).
import fs from "fs";
import path from "path";
const file = path.join("src", "app.ts");
const text = fs.readFileSync(file, "utf8");
console.log(text.length);3. The form that works even with the flag off.
// 3. The form that works even with the flag off.
import * as fs from "fs";
fs.readFileSync("package.json", "utf8");Common Gotchas
The flag does not fix a package whose types disagree with its JS
if `.d.ts` says `export default function` and the file is `exports.foo = …`, you will still get a runtime `default is not a function`.
`allowSyntheticDefaultImports` is types only
it will not change emit. `esModuleInterop` is the one that adds the helper.