Error [ERR_REQUIRE_ESM]: require() of ES Module not supported
Quick answer
You're using require() on a package that only ships ES Modules (it declares "type": "module" or only exports .mjs). Either switch your file to ESM and use import, or use a dynamic import() instead of require().
What causes this error
Node's CommonJS require() cannot synchronously load an ES Module. If a package's package.json declares "type": "module", or it only ships .mjs files, calling require('that-package') throws ERR_REQUIRE_ESM — even though import works fine for the same package.
The Fix
// Wrong — CommonJS file requiring an ESM-only package
const chalk = require('chalk'); // throws ERR_REQUIRE_ESM
// Fix 1: dynamic import (works from a CommonJS file too)
const { default: chalk } = await import('chalk');
// Fix 2: convert your own file/project to ESM
// package.json: "type": "module"
import chalk from 'chalk';Common causes / variations
- Upgrading a dependency (chalk, node-fetch, execa, nanoid, etc.) to a major version that dropped CommonJS support entirely
- Mixing CommonJS (default for `.js`) and ESM-only packages in the same project without a migration plan
- Running TypeScript through a CommonJS-mode transpiler against an ESM-only dependency
Related errors
See also: Cannot find module (missing file extension in ESM), Unknown file extension ".ts".
Was this fix helpful?
Comments
Loading comments...