---
title: "Error [ERR_REQUIRE_ESM]: require() of ES Module not supported"
description: "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."
url: https://www.danny.engineering/answer/nodejs/error-err_require_esm-require-of-es-module-not-supported
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T17:25:00.000Z
tags: ["Node.js"]
---

# Error [ERR_REQUIRE_ESM]: require() of ES Module not supported

## 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.


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

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:


```bash
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.


```json
{
  "engines": {
    "node": ">=22.12.0"
  }
}
```


### 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:


```javascript
let nanoid;

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


### 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`:


```diff
- const express = require('express');
- const chalk = require('chalk');
+ import express from 'express';
+ 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:


```bash
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`.

## Related reading

- [Node.js docs: Loading ECMAScript modules using require()](https://nodejs.org/api/modules.html#loading-ecmascript-modules-using-require)
- [Node.js docs: Modules, ECMAScript modules](https://nodejs.org/api/esm.html)
