ERR_REQUIRE_ESM 🟩 Node.js

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…).

Seen on: Node.js npm

Meaning

Many packages dropped CommonJS in recent major versions. Older Node versions can’t require() ESM; newer Node (22+) supports require(esm) only for synchronous modules.

Common causes

  • Upgraded a dependency to an ESM-only major version
  • Project is CommonJS (no "type": "module")
  • TypeScript compiling imports to require()

⚡ Quick fix

  1. Switch the project to ESM ("type": "module") and use import
  2. Or use dynamic import: const { default: fetch } = await import('node-fetch')
  3. Or pin the last CommonJS version of the package
  4. Node 18+: use the built-in fetch instead of node-fetch

Detailed fix by platform

Node.js

  1. Dynamic import from CommonJS:
    javascript
    // CommonJS file
    async function main() {
      const { default: chalk } = await import('chalk');
      console.log(chalk.green('ok'));
    }
    main();

How to diagnose

  1. Package — Which package and version?
  2. Module type — package.json "type"? TS module setting?

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.