TypeScript - Node Interop: ESM/CJS
Choose a module system
- ESM (modern): package.json
"type": "module"
,module
ESNext
,moduleResolution
nodenext
orbundler
. - CJS (legacy): omit
type
or set"type": "commonjs"
,module
CommonJS
.
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
esModuleInterop
andallowSyntheticDefaultImports
ease default import interop with CJS packages.
File extensions
When emitting ESM for Node, include .js
extensions in imports (import {x} from './x.js'
).