Node.js · Modules

ERR_REQUIRE_ESM: require() of ES Module

The package you are loading only supports import, and your bot loads it with require. It usually appears after installing the latest version of a package an older tutorial used.

← All errors

What you are seeing

Node.js

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/container/node_modules/node-fetch/src/index.js from /home/container/index.js not supported.

Why it happens

Node has two module systems: CommonJS, with require, and ES modules, with import. Several popular packages dropped CommonJS in a major release — node-fetch 3, chalk 5 and got 12 among them — so a tutorial that says require('node-fetch') works with the version it was written against and fails with today's.

Newer Node releases, from 22.12, can require most ES modules directly, so this error is less common than it was. On older versions, pick one of the fixes below.

The fixes

Do you need the package at all? Node 18 and later have fetch built in, so node-fetch can simply be removed.

Install the last CommonJS version: node-fetch@2, chalk@4 or got@11. They still work, and nothing else in your code has to change.

Load it with a dynamic import, which works from CommonJS.

Or move the whole project to ES modules by adding "type": "module" to package.json and changing every require to import.

Built-in fetch

// No package needed on Node 18 or later.
const response = await fetch('https://api.example.com/data');
const data = await response.json();

Dynamic import

// Works from a CommonJS file, inside an async function.
const { default: chalk } = await import('chalk');

FAQ

Questions

Is converting to ES modules worth it?

For a new bot, yes; it is where the ecosystem is going. For an existing bot that works, pinning the older package version is the quicker fix and perfectly sound.