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

MessageWhat it meansUsual fix
ERR_REQUIRE_ESMCommonJS code called require() on an ES module, on a Node.js version that does not allow itUse 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 pathESM import without a file extensionESM in Node.js needs full relative paths: ./util.js, not ./util
ERR_PACKAGE_PATH_NOT_EXPORTEDImporting a subpath the package's exports map does not listImport a documented entry point, or (author) add the subpath to exports
X is not a function after importDefault export mismatch: CommonJS module.exports seen as default, or a transpiled default propertyTry mod.default; as author, avoid mixing default and named exports across formats
require is not defined in ES module scopeA .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:

  1. Does every exports entry have the right conditions? A CommonJS user needs a require (or default) target that is actually CommonJS.
  2. Do the files match their declared format? A .js file containing export under a package without "type": "module" fails for everyone.
  3. 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.