Troubleshooting ERR_REQUIRE_ESM and Other Module Format Errors
Node.js has two module systems, CommonJS and ECMAScript modules, and packages sit on either side of the line or straddle it. When the two meet badly the errors look cryptic. This page is a symptom-first guide: find your error message, read what it means, and decide whether the fix belongs in your package, in a dependency, or in the importing code.
First, know which format each file is
Node.js decides per file. A .mjs file is always ESM and a .cjs file is always CommonJS. A plain .js file follows the nearest package.json: ESM if it says "type": "module", CommonJS otherwise. Write down the format of both the importing file and the file being loaded before you change anything; most fixes become obvious once you have.
Error by error
| Message | What it means | Usual fix |
|---|---|---|
ERR_REQUIRE_ESM | CommonJS code called require() on an ES module, on a Node.js version that does not allow it | Use dynamic import(), upgrade to a Node.js line that supports requiring ESM, or (as the package author) ship a CommonJS build too |
ERR_MODULE_NOT_FOUND for a relative path | ESM import without a file extension | ESM in Node.js needs full relative paths: ./util.js, not ./util |
ERR_PACKAGE_PATH_NOT_EXPORTED | Importing a subpath the package's exports map does not list | Import a documented entry point, or (author) add the subpath to exports |
X is not a function after import | Default export mismatch: CommonJS module.exports seen as default, or a transpiled default property | Try mod.default; as author, avoid mixing default and named exports across formats |
require is not defined in ES module scope | A .js file is treated as ESM because of "type": "module" | Rename to .cjs or convert to import |
Recent Node.js lines added support for require() of synchronous ES modules, which removes the first error in many cases. Check the Node.js documentation for the versions you support rather than assuming every user has it.
Is it your package?
If users report one of these errors against your package, check three things in the published tarball:
- Does every
exportsentry have the right conditions? A CommonJS user needs arequire(ordefault) target that is actually CommonJS. - Do the files match their declared format? A
.jsfile containingexportunder a package without"type": "module"fails for everyone. - Do relative imports inside your ESM build carry extensions? Bundlers tolerate missing extensions; Node.js does not.
{
"type": "module",
"exports": {
".": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
}
}
What the result means and when to go further
If the importing code and your package agree on formats and the error persists, the cause is often a tool in between: a test runner, bundler or TypeScript setting that rewrites imports. Reproduce with plain node first. If plain Node.js works, report the issue to that tool with a minimal reproduction, as described in writing a minimal bug reproduction. To design the package side properly, see the dual ESM and CommonJS tutorial and the notes on ESM-only packages.