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…).
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).
| ERR_REQUIRE_ESM | import outside module | |
|---|---|---|
| Your code uses | require() | import |
| Problem | The package is ESM-only | Your file is treated as CommonJS |
| Fix | Use import()/switch to ESM/pin older version | Add "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.