cjs-resolves-esm
Detect when CJS require() would resolve to an ESM file, causing ERR_REQUIRE_ESM at runtime.
| Rule ID | types/cjs-resolves-esm |
| Category | types |
| Default Severity | 🔴 error |
| Fixable | No |
| attw equivalent | CJSResolvesToESM |
What it checks
This rule detects packages where a CommonJS require() call would resolve to an ESM file. Node.js throws ERR_REQUIRE_ESM when this happens, breaking CJS consumers.
Common patterns caught:
type: "module"with string exports —require()resolves to a.jsfile that is ESMrequirecondition pointing to.mjs—.mjsis always ESMrequirecondition pointing to.jswithtype: "module"—.jsis ESM whentype: "module"defaultfallback to ESM — norequirecondition, CJS falls back todefaultwhich is ESM
Examples
Fail
json
{
"type": "module",
"exports": "./source/index.js"
}json
{
"exports": {
".": {
"require": "./dist/index.mjs"
}
}
}json
{
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"default": "./dist/index.js"
}
}
}Pass
json
{
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
}
}json
{
"exports": {
".": {
"import": "./dist/index.mjs",
"require": "./dist/index.cjs"
}
}
}How to fix
Provide a CJS-compatible file for the require condition:
- Use
.cjsextension for CJS files whentype: "module" - Add a separate
requirecondition pointing to a CJS build - Use a bundler (like
tspub build --format esm,cjs) to generate both formats