Node.js ERR_REQUIRE_ESM: Causes and Fixes
If require() stops with Error [ERR_REQUIRE_ESM] after you upgrade an npm package to a new major version, the package is published only as an ES module (a format written with import / export). Older versions of Node.js cannot load ES modules with require(). Load the package with import(), or turn your own file into an ES module, and the error goes away.
Whether you see this error depends on the Node.js version. Node.js 22.12.0 and 20.19.0 added support for loading ES modules with require(), so the same code no longer fails with this error. However, a different error can appear instead. I ran the same file on four versions of Node.js using chalk 5, an ES-module-only package. I recorded the full text of the error, and confirmed that each fix makes it go away.
The test environment was Ubuntu 24.04. I ran the same file on Node.js 20.18.3, 22.11.0, 22.23.4, and 24.22.1. For 20.18.3 and 22.11.0, I used the Linux binaries distributed at nodejs.org. The packages were chalk 5.6.2 and 4.1.2, installed with npm 11.19.1. I did not test Windows or macOS, a case where you transpile TypeScript before running it, or the same error inside tools such as Jest or webpack.
Full error output (Node.js 20.18.3)
In the test environment, I ran npm init -y, removed the type line from package.json (with npm pkg delete type), and then installed chalk 5.6.2 with npm install chalk@5. The package.json for chalk 5 has "type": "module", and its exports field points to a single ES module file.
$ grep -E '"(version|type|exports)"' node_modules/chalk/package.json
"version": "5.6.2",
"type": "module",
"exports": "./source/index.js",
I load this chalk from an index.js written in CommonJS (the format that uses require and has been part of Node.js from the start).
const chalk = require('chalk');
console.log(chalk.blue('hello'));
When I ran node index.js on Node.js 20.18.3, it stopped as shown below. The exit code was 1. I replaced the paths with example values.
$ node index.js
/path/to/app/index.js:1
const chalk = require('chalk');
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/app/node_modules/chalk/source/index.js from /path/to/app/index.js not supported.
Instead change the require of /path/to/app/node_modules/chalk/source/index.js in /path/to/app/index.js to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/path/to/app/index.js:1:15) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v20.18.3
The line starting with Error [ERR_REQUIRE_ESM] means that index.js cannot require() the ES module chalk/source/index.js. The next line, Instead change …, gives the fix: change that require to a dynamic import(), which works in every CommonJS module. The index.js:1 location and the ^ marker show that the failure occurred at require('chalk') on line 1.
Node.js 22.11.0 printed the same message, but the location was shown as index.js:315 and undefined, not the require on line 1. The actual call site is visible in the stack line at Object.<anonymous> (/path/to/app/index.js:1:15).
$ node index.js
/path/to/app/index.js:315
undefined
^
Error [ERR_REQUIRE_ESM]: require() of ES Module /path/to/app/node_modules/chalk/source/index.js from /path/to/app/index.js not supported.
Instead change the require of /path/to/app/node_modules/chalk/source/index.js in /path/to/app/index.js to a dynamic import() which is available in all CommonJS modules.
at TracingChannel.traceSync (node:diagnostics_channel:315:14)
at Object.<anonymous> (/path/to/app/index.js:1:15) {
code: 'ERR_REQUIRE_ESM'
}
Node.js v22.11.0
Differences by Node.js version
These are the results of running the same index.js on the four versions.
| Node.js | Result of node index.js |
|---|---|
| 20.18.3 | Stops with ERR_REQUIRE_ESM |
| 22.11.0 | Stops with ERR_REQUIRE_ESM |
| 22.23.4 | Stops with TypeError: chalk.blue is not a function |
| 24.22.1 | Stops with TypeError: chalk.blue is not a function |
According to the official documentation on loading ES modules with require(), this feature was added in Node.js 22.0.0 and 20.17.0. At first it was available only with the --experimental-require-module flag. It has been enabled by default since Node.js 23.0.0, 22.12.0, and 20.19.0. The difference explains why 22.11.0 and earlier show the error, while 22.23.4 shows a different one. Node.js 24.15.0 and later no longer treat the feature as experimental.
The description of ERR_REQUIRE_ESM also says that from Node.js 22.12.0 and 20.19.0 onward, synchronous ES modules can be loaded with require() by default, and it marks this error as deprecated.
Newer Node.js versions can still produce this error. On Node.js 24.22.1, I ran index-default.js, which is introduced later, with --no-require-module. It produced the same ERR_REQUIRE_ESM as index-default.js on 20.18.3. The official documentation describes this flag as a way to disable the feature if it causes unexpected problems.
The "is not a function" error on newer Node.js
On Node.js 22.23.4 and 24.22.1, require('chalk') succeeded, but execution stopped on the next line. This is the output from Node.js 24.22.1. I replaced the paths with example values.
$ node index.js
/path/to/app/index.js:3
console.log(chalk.blue('hello'));
^
TypeError: chalk.blue is not a function
at Object.<anonymous> (/path/to/app/index.js:3:19)
at Module._compile (node:internal/modules/cjs/loader:1929:14)
at Object..js (node:internal/modules/cjs/loader:2060:10)
at Module.load (node:internal/modules/cjs/loader:1651:32)
at Module._load (node:internal/modules/cjs/loader:1443:12)
at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:154:5)
at node:internal/main/run_main_module:33:47
Node.js v24.22.1
When require() loads an ES module, it returns a namespace object that holds the module's exported names, not the module's contents directly. Anything exported with export default is placed under default. When I printed the keys of the returned object, they included default, Chalk, and __esModule, but not blue. The type of mod.default.blue was function.
const mod = require('chalk');
console.log(Object.keys(mod));
console.log(typeof mod.default.blue);
$ node inspect.js
[
'Chalk',
'__esModule',
'backgroundColorNames',
'backgroundColors',
'chalkStderr',
'colorNames',
'colors',
'default',
'foregroundColorNames',
'foregroundColors',
'modifierNames',
'modifiers',
'supportsColor',
'supportsColorStderr'
]
function
So I created index-default.js, which adds .default to the require call.
const chalk = require('chalk').default;
console.log(chalk.blue('hello'));
On Node.js 22.23.4 and 24.22.1, this printed hello with no errors or warnings. On 20.18.3 and 22.11.0, it produced ERR_REQUIRE_ESM. In those versions, the Instead change … line read Instead change the require of index.js in …, naming only the file. This form works only on Node.js versions that can require() ES modules.
Node.js 22.11.0 printed hello for index-default.js when run with --experimental-require-module. It also printed the warning below. (node:1691) is the process ID.
$ node --experimental-require-module index-default.js
hello
(node:1691) ExperimentalWarning: Support for loading ES Module in require() is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
The warning says that loading ES modules with require() is experimental and may change at any time. According to the official documentation, this warning is no longer shown by default from Node.js 22.13.0 and 20.19.0.
Fixes and confirming the error goes away
I confirmed that each of the following fixes removes the error and prints hello. If you need the code to run on every version, use either import() or an ES module conversion.
Keep CommonJS and load with import()
This is the approach suggested by the Instead change … line in the error. import() returns a Promise, so you wait for it with await inside an async function. A default export is accessed by its default name.
(async () => {
const { default: chalk } = await import('chalk');
console.log(chalk.blue('hello'));
})();
This index-dynamic.js printed hello on all four versions: 20.18.3, 22.11.0, 22.23.4, and 24.22.1. You don't need to change package.json or any other files. However, the code that uses chalk must run after the await.
Make your own file an ES module
ES-module-only packages are meant to be loaded with import from ES module files. I created index.mjs, with the .mjs extension.
import chalk from 'chalk';
console.log(chalk.blue('hello'));
node index.mjs printed hello on all four versions. If you want the whole project to use ES modules, you can keep the .js extension and add "type": "module" to package.json (official documentation on the "type" field). In that case, you also need to change any require calls in other files to import.
Go back to a CommonJS release of the package
If you can't rewrite the code right away, you can install a CommonJS release of the package. With npm install chalk@4, which installs 4.1.2, the original index.js printed hello on both 20.18.3 and 24.22.1. However, this means you keep using an older major version of chalk.
Top-level await means require() cannot load it
Even on newer Node.js versions, some ES modules cannot be loaded with require(). These are modules that use await outside a function (top-level await). For testing, I created a package named tla-pkg in node_modules, with the following index.js (its package.json has "type": "module").
const config = await Promise.resolve({ name: 'neko' });
export default config;
When I loaded it with require('tla-pkg').default on Node.js 24.22.1, the error was ERR_REQUIRE_ASYNC_MODULE, not ERR_REQUIRE_ESM. I omitted the lines from at … onward after - /path/to/app/tla.js, and I replaced the paths with example values.
$ node tla.js
node:internal/modules/esm/module_job:313
throw new ERR_REQUIRE_ASYNC_MODULE(filename, parent, locations);
^
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.
Required module: /path/to/app/node_modules/tla-pkg/index.js
Require stack:
- /path/to/app/tla.js
The message means: "require() cannot be used on an ES module that contains top-level await. Use import() instead." On Node.js 22.23.4, the first line of the error was the same, but the lines below it were From /path/to/app/tla.js and Requiring /path/to/app/node_modules/tla-pkg/index.js, which is formatted differently from 24.22.1. On 20.18.3, the error was ERR_REQUIRE_ESM, the same as with chalk.
As described in the ERR_REQUIRE_ASYNC_MODULE documentation, this error occurs when the module being loaded is asynchronous. The fix is the import() that the message suggests. I changed the code to await import('tla-pkg'), using the same form as before, and it printed neko on Node.js 24.22.1.
If you upgrade to Node.js 22.12.0 or later, or 20.19.0 or later, ERR_REQUIRE_ESM no longer appears. However, the shape of the value returned by require() changes, so you still need to make changes such as adding .default. Packages that contain top-level await can be loaded only with import(), even on newer Node.js versions.





