ERR_REQUIRE_ESM vs "Cannot use import statement outside a module"

Two sides of the same ESM/CommonJS mismatch: CommonJS code requiring an ESM package, vs ESM syntax running in a CommonJS context.

ERR_REQUIRE_ESM

Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

CommonJS code used require() on a package that is ES-module-only (node-fetch 3, chalk 5, nanoid 4, got 12…).

Causes & fixes →
import outside module

SyntaxError: Cannot use import statement outside a module

An import statement ran in a context treated as CommonJS (Node without "type": "module", a browser script without type="module", or Jest without ESM transform).

Causes & fixes →
ERR_REQUIRE_ESMimport outside module
Your code usesrequire()import
ProblemThe package is ESM-onlyYour file is treated as CommonJS
FixUse import()/switch to ESM/pin older versionAdd "type": "module", .mjs, or type="module"

Rule of thumb

Decide whether the project is ESM or CommonJS, then make files, package.json and tooling agree.