# Error [ERR_REQUIRE_ESM]: require() of ES Module not supported: how to fix it

> Error [ERR_REQUIRE_ESM]: require() of ES Module not supported means CommonJS code called require() on a package that ships only as an ES module, as node-fetch 3, chalk 5 and nanoid 4 do. Node.js 22.12.0 and 20.19.0 and later can require() such a module unless it uses top-level await, which throws ERR_REQUIRE_ASYNC_MODULE instead. Otherwise switch the project to ES modules, load the package with import(), or pin the last CommonJS major.

Source: https://livops.dev/en/guides/error-err-require-esm

By Livops team, the people who build and run Livops. Updated 1 December 2026.

As Node.js prints it, here with `require()` of ES modules turned off (shortened; paths vary):

```text
/app/src/index.js:1
const fetch = require("node-fetch");
              ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /app/node_modules/node-fetch/src/index.js from /app/src/index.js not supported.
Instead change the require of index.js in /app/src/index.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (/app/src/index.js:1:15) {
  code: 'ERR_REQUIRE_ESM'
}
```

And what a current Node.js prints in its place when the module uses top-level `await`:

```text
Error [ERR_REQUIRE_ASYNC_MODULE]: require() cannot be used on an ESM graph with top-level await. Use import() instead. To see where the top-level await comes from, use --experimental-print-required-tla.
  From /app/src/index.js
  Requiring /app/src/config.mjs
```

This page explains why a package you have used for years suddenly cannot be loaded with `require()`, what changed in Node.js so that newer versions load it anyway, and the four fixes, from the one that changes the least. The last section shows where it surfaces on Livops.

## What does the error mean?

Node.js has two module systems: CommonJS, with `require()`, and ES modules, with `import`[3]. `ERR_REQUIRE_ESM` means CommonJS code called `require()` on a file that is an ES module[2]: a `.mjs` file, or a `.js` file under a `package.json` with `"type": "module"`[4].

Usually that file is not yours. It is a dependency whose new major version is published only as an ES module:

| Package | ES module only from | What the project says |
| --- | --- | --- |
| `node-fetch` | v3 | v3 cannot be loaded with `require()`; if you cannot switch to ESM, use v2[6] |
| `chalk` | v5 | Chalk 5 is pure ESM[8]; with TypeScript or a build tool, you probably want Chalk 4[7] |
| `nanoid` | v4 | Nano ID 4 works only with ESM; the 3.x branch keeps CommonJS[9] |

Many other packages made the same move. The error message names the file; the folder under `node_modules` tells you which package it is.

## Node.js can now require() ES modules

The error belongs to older versions of Node.js. `require()` loads ES modules without a flag from **Node.js 23.0.0, 22.12.0 and 20.19.0**[1], and the Node.js docs mark `ERR_REQUIRE_ESM` as deprecated since then[2]. Node.js 24 has it in every release. The same versions are given in [Cannot use import statement outside a module](https://livops.dev/en/guides/error-cannot-use-import-statement-outside-module), which covers the opposite mix-up.

Three things to know before you rely on it:

- **No top-level `await`.** `require()` only loads ES modules that are fully synchronous. If the module, or anything it imports, uses top-level `await`, Node.js throws `ERR_REQUIRE_ASYNC_MODULE`, and the docs say to load it with `import()` instead[1][2].
- **The default export is in `.default`.** `require()` returns the module's namespace object, like `import()` does, with the default export in its `default` property[1]. Code written for the old CommonJS version then fails one line later with `TypeError: fetch is not a function`. A package can choose to return one value directly by exporting it under the name `"module.exports"`[1], but you cannot count on that.

  ```js
  const fetch = require("node-fetch").default;
  const chalk = require("chalk").default;
  const { nanoid } = require("nanoid");     // named exports work as they are
  ```

- **You can check for it.** `process.features.require_module` is `true` when the feature is on[1], and `--no-require-module` turns it off[1], which is how the first example above was printed.

## The fixes, from the smallest change

### 1. Run a Node.js version that can require() ES modules

If the code works on your machine and fails on the server, compare `node --version` in both places first. A server on Node.js 20.18 or 22.11 throws `ERR_REQUIRE_ESM` where your Node.js 24 does not. Pin the version in the project so both run the same one:

```sh
echo "24" > .nvmrc
```

Then add `.default` where a package's default export is used, as shown above.

### 2. Switch the project to ES modules

The fix that removes the problem for every package at once. Add `"type": "module"` to `package.json` and every `.js` file is loaded as an ES module[4]; then replace `require()` with `import`:

```json
{
  "type": "module",
  "scripts": {
    "start": "node src/index.js"
  }
}
```

```js
import fetch from "node-fetch";
import chalk from "chalk";
```

Files that must stay CommonJS, such as some config files, keep working if you rename them to `.cjs`, which is always CommonJS[4]. What else changes (`__dirname`, file extensions in imports) is in [Cannot use import statement outside a module](https://livops.dev/en/guides/error-cannot-use-import-statement-outside-module).

### 3. Load the package with import() from CommonJS

Dynamic `import()` works in CommonJS too, and it loads ES modules[3], with or without top-level `await`. It returns a promise, so it fits where the value is used inside an `async` function. node-fetch's own README gives this one-liner for a `.cjs` file[6]:

```js
const fetch = (...args) => import("node-fetch").then(({ default: fetch }) => fetch(...args));
```

The same pattern for a module you need once at startup:

```js
async function main() {
  const { default: chalk } = await import("chalk");
  console.log(chalk.green("ready"));
}
main();
```

**In TypeScript**, check the `module` setting first. With `"module": "commonjs"`, `tsc` turns `import()` into a promise of a `require()` call[10], so the error comes back in the built file. With `"module": "nodenext"`, `import()` is left as it is in CommonJS output[10], and `require()` of an ES module is allowed, to match Node.js 22.12.0 and later[10].

### 4. Pin the last CommonJS major of the package

A stopgap when you cannot change Node.js or the code today. Each project names the line that keeps `require()` working:

```sh
npm install node-fetch@2    # node-fetch: v2 remains compatible with CommonJS
npm install chalk@4         # chalk: "totally fine to stay on Chalk v4"
npm install nanoid@3        # nanoid: 3.x is supported for CommonJS users
```

node-fetch says it keeps fixing critical bugs in v2[6], chalk calls v4 stable for years[8], and Nano ID keeps the 3.x branch for users who cannot migrate to ESM[9]. You give up new features, and any package that goes ESM-only later brings the error back.

**For node-fetch, there is a better option:** Node.js has its own `fetch()`, without a flag since Node.js 18.0.0 and no longer experimental since 21.0.0[5]. Delete the `require("node-fetch")` line and the package.

### When the error comes from a tool, not your code

If the stack trace shows a CLI, a test runner or a bundler plugin calling `require()`, the fix is usually an upgrade of that tool, or running it on a newer Node.js. The package it fails on is the one named after `require() of ES Module`.

## How to confirm the fix locally

Run what the server runs, from a fresh clone, with the server's Node.js version:

```sh
git clone <your repo> /tmp/check && cd /tmp/check
node --version
node -p "process.features.require_module"   # true: require() can load ES modules
npm ci && npm run build
npm start
```

To try another Node.js version without installing it:

```sh
docker run --rm -v "$PWD":/app -w /app node:24 sh -c "npm ci && npm run build && npm start"
```

## On Livops

- **Where it shows.** The build passes, the app stops right after it starts, and the deploy fails as `start_failed`. The **Logs** page shows the app's last words, `ERR_REQUIRE_ESM` among them, often with a **Tip:** line under it. A web app's previous version keeps running; a bot's previous version is stopped first, so the bot is down until a deploy succeeds. See [The app did not start](https://livops.dev/en/docs/error-start).
- **Node.js versions.** Livops has Node.js 22 and 24. It reads `.nvmrc` and `.node-version`, and `devEngines.runtime.version` in `package.json` can ask for 22 or 24. Ask for 24 if your code relies on `require()` of ES modules. See [Install failed](https://livops.dev/en/docs/error-install).
- **The start command.** Set it in **Settings → Start**, or in a `Procfile` at the project root: `web:` for a site or an API, `worker:` for a bot. In `livops.json`, `start` is applied when the project is created; after that it is changed in the project's settings. For TypeScript, build first and start the built file, for example `node dist/main.js`. See [No start command](https://livops.dev/en/docs/error-no-start).

  ```json
  {
    "build": "npm run build",
    "start": "node dist/index.js"
  }
  ```

A start that fails with `Cannot find module` instead is in [Error: Cannot find module](https://livops.dev/en/guides/error-node-cannot-find-module), and running a Node.js server in production in [Node.js hosting](https://livops.dev/en/hosting/nodejs-hosting).

## Questions

**Why did ERR_REQUIRE_ESM appear after I updated a package?**

The new major version of the package is published only as an ES module. node-fetch from v3, chalk from v5 and nanoid from v4 made that change. Your code still calls require(), and an older Node.js cannot require() an ES module.

**Which Node.js versions can require() an ES module?**

Node.js 22.12.0 and later, 20.19.0 and later, and 23.0.0 and later do it by default, without a flag. Node.js 24 has it in every release. The module must not use top-level await.

**What is ERR_REQUIRE_ASYNC_MODULE?**

The error a current Node.js throws instead of ERR_REQUIRE_ESM when the ES module you require(), or anything it imports, uses top-level await. require() is synchronous and cannot wait for it. Load that module with import() instead.

**Why is fetch not a function after require works?**

require() of an ES module returns its namespace object, and the default export is in its default property. Write require("node-fetch").default, or on Node.js 18 and later drop node-fetch and use the built-in fetch.

**Should I downgrade to node-fetch 2 or chalk 4?**

It works, and both projects say so: node-fetch v2 stays compatible with CommonJS and gets critical fixes, and chalk says it is fine to stay on v4. Treat it as a stopgap. A newer Node.js or a move to ES modules removes the problem for every package at once.

**My TypeScript code uses import, so why does it say require()?**

tsc with module commonjs rewrites import into require(), and even turns import() into a require() call. Use module nodenext: it keeps import() as it is, and it allows require() of an ES module the way Node.js 22.12 and later do.

**Which Node.js version does Livops run?**

Node.js 22 or 24. You choose with .nvmrc, .node-version or devEngines.runtime.version in package.json. Ask for 24 if your code relies on require() of ES modules.

## Sources

1. [Node.js Docs — Modules: CommonJS modules](https://nodejs.org/api/modules.html). Checked 1 December 2026.
2. [Node.js Docs — Errors](https://nodejs.org/api/errors.html). Checked 1 December 2026.
3. [Node.js Docs — Modules: ECMAScript modules](https://nodejs.org/api/esm.html). Checked 1 December 2026.
4. [Node.js Docs — Modules: Packages](https://nodejs.org/api/packages.html). Checked 1 December 2026.
5. [Node.js Docs — Global objects (fetch)](https://nodejs.org/api/globals.html). Checked 1 December 2026.
6. [node-fetch — README](https://github.com/node-fetch/node-fetch). Checked 1 December 2026.
7. [Chalk — README](https://github.com/chalk/chalk). Checked 1 December 2026.
8. [Chalk — v5.0.0 release notes](https://github.com/chalk/chalk/releases/tag/v5.0.0). Checked 1 December 2026.
9. [Nano ID — Changelog](https://github.com/ai/nanoid/blob/main/CHANGELOG.md). Checked 1 December 2026.
10. [TypeScript Handbook — Modules: Reference](https://www.typescriptlang.org/docs/handbook/modules/reference.html). Checked 1 December 2026.
