More Node.js answers

by Danny
Published 3 min read

You called require() on a package that ships only as an ES module. Upgrade Node.js, switch your code to ESM, or load the package with dynamic import() to fix it.

The short answer

#

Your CommonJS code is calling require() on a package that is published as an ES module only. Current Node.js releases can require() most ES modules, so the first fix is to upgrade to a supported version (Node.js 22.12.0 or later). If you cannot, load the package with dynamic import() or move your own code to ES modules.


_10
// works in any CommonJS file, on any supported Node.js version
_10
async function main() {
_10
const { default: chalk } = await import('chalk');
_10
console.log(chalk.green('Loaded'));
_10
}
_10
_10
main();

Why it happens

#

Node.js has two module systems. CommonJS uses require() and module.exports. ES modules (ESM) use import and export. A file is treated as ESM when it ends in .mjs, or ends in .js inside a package whose package.json has "type": "module".

For years, require() could only load CommonJS. ES modules can be loaded asynchronously and can use top-level await, which a synchronous require() call could not wait for, so Node.js refused and threw ERR_REQUIRE_ESM. Many popular packages, such as chalk, node-fetch and nanoid, moved to ESM only in their newer major versions, which is why the error often appears straight after an npm update.

That changed with require(esm). It is enabled by default from Node.js 22.12.0 and 20.19.0, and in later releases. On those versions, require() can load an ES module as long as the module graph does not use top-level await. Node.js 20 reached end of life in April 2026, so in practice that means Node.js 22 or newer.

You can still see the error, or a close relative of it, when:

  • You run an older Node.js version, often in CI or a Docker base image that has not been updated.
  • The ES module, or something it imports, uses top-level await. You then get ERR_REQUIRE_ASYNC_MODULE instead.
  • A tool does its own module loading rather than using Node.js directly, for example an older Jest setup, ts-node, or a bundler configured for CommonJS output.

How to fix it

#

Upgrade Node.js

Check the version you are actually running, in the same place the error happens:


_10
node --version

If it is older than 22.12.0, upgrade to a supported release line. Node.js 26 is the active LTS line and Node.js 22 is in maintenance until April 2027. Remember to update CI images, Dockerfiles and the engines field in package.json as well as your laptop.


_10
{
_10
"engines": {
_10
"node": ">=22.12.0"
_10
}
_10
}

Use dynamic import() from CommonJS

import() works in CommonJS files and returns a promise. It is the safest fix when you must stay on CommonJS or support older runtimes. The default export is on the default property:


_10
let nanoid;
_10
_10
async function getId() {
_10
if (!nanoid) {
_10
({ nanoid } = await import('nanoid'));
_10
}
_10
return nanoid();
_10
}

Move your project to ES modules

If you are starting fresh or can afford the change, switch your own code to ESM. Add "type": "module" to package.json and replace require() calls with import:


_10
- const express = require('express');
_10
- const chalk = require('chalk');
_10
+ import express from 'express';
_10
+ import chalk from 'chalk';

Expect a few follow-on changes. Relative imports need their file extension (./utils.js, not ./utils), and __dirname is no longer defined, so use import.meta.dirname instead.

Pin the last CommonJS version

As a short-term fix, install the last major version that still shipped CommonJS. Check the package changelog for which version that is. For example, chalk 4 and node-fetch 2 are CommonJS:


_10
npm install chalk@4

Treat this as temporary. Older majors stop getting fixes, and for node-fetch you can drop the package entirely because fetch is built into Node.js.

Common pitfalls

#
  • Upgrading Node.js locally but not in CI or production, so the error moves rather than going away.
  • Forgetting .default when using import() on a package with a default export.
  • Using TypeScript with "module": "commonjs" output. TypeScript turns your import statements into require() calls, which brings the error back on older runtimes. Use "module": "nodenext" so the output matches what Node.js expects.
  • Seeing the error from a test runner rather than Node.js itself. Check your test runner's ESM support and configuration before changing application code.
  • Adding top-level await to a shared ES module that CommonJS code still requires. That breaks require(esm) with ERR_REQUIRE_ASYNC_MODULE.
#

Was this resource helpful?