TypeScript - Node Interop: ESM/CJS
Choose a module system
- ESM (modern): package.json "type": "module",moduleESNext,moduleResolutionnodenextorbundler.
- CJS (legacy): omit typeor set"type": "commonjs",moduleCommonJS.
Example configs
// package.json (ESM)
{
  "type": "module",
  "exports": {
    ".": {
      "import": "./dist/index.js",
      "require": "./dist/index.cjs"
    }
  }
}
// tsconfig.json (NodeNext)
{
  "compilerOptions": {
    "module": "NodeNext",
    "moduleResolution": "NodeNext",
    "target": "ES2020",
    "outDir": "dist"
  }
}
Interop flags
- esModuleInteropand- allowSyntheticDefaultImportsease default import interop with CJS packages.
File extensions
When emitting ESM for Node, include .js extensions in imports (import {x} from './x.js').