It is because of the node-fetch package. As recent versions of this package only supports ESM; you have to downgrade it to an older version [email protected] or lower.

npm i [email protected]

This should solve the issue.

Answer from ABDULLOKH MUKHAMMADJONOV on Stack Overflow
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Operators › import
import() - JavaScript - MDN Web Docs - Mozilla
Unlike the declaration-style counterpart, dynamic imports are only evaluated when needed, and permit greater syntactic flexibility. ... The import() call is a syntax that closely resembles a function call, but import itself is a keyword, not a function. You cannot alias it like const myImport = import, which will throw a SyntaxError. Trailing commas are only allowed if the runtime also supports options. Check browser compatibility. ... The module to import from.
🌐
Reddit
reddit.com › r/node › dynamic import() forcing me to stick with commonjs over esm???
r/node on Reddit: Dynamic import() forcing me to stick with CommonJS over ESM???
June 30, 2023 -

I've been in development of a library that is to be used as a framework for web applications. I've been using CommonJS for over a decade, but I felt like it was time to make the switch and rebuild my library using ESM format, BUT I need to be able to support both methods for implementation. After extensive reading and a bit of playing around, it seems like I will be unable to move to ESM and keep it backwards compatible with CommonJS without using a transpiler. That sucks. If anyone has any suggested ways of doing this, I'm all ears.

For context, I want to be able to import or require both named and default exports, such as:

// from an ESM module
import LibName, { Foo, Bar } from 'my-lib

// from a CJS file
const LibName = require('my-lib')
// or
const { Foo, Bar } = require('my-lib')

I've started playing around with methods to get around it, but nothing seems to work. Creating multiple exports in my package.json sounded like a good way to go, but that basically requires the base code to be in CJS. Using ESM doesn't provide a way to create a CJS export "shim" because there's no way to dynamically import() and then export from a CJS file.

I know, a bunch of you will tell me to "forget about CJS, it's the past", but in order to make a library consumable by the masses, you need to be able to support both. I'm sure I'll also get advice about building it in Typescript, but I've tried it and I'm not a fan (I would love syntax for types built into JavaScript, but one of the great things about JS is that it doesn't need to be compiled and, well, TS fundamentally changes that).

I REALLY want to make the switch, but without full backwards compatibility to do everything the same from a client-code perspective, there just doesn't seem to be a way around it. I'm going to have to do it (again) using CommonJS formatting and then make it better compatible with ESM imports. Any advice?

Top answer
1 of 2
6
Hey OP, I do understand your pains! I'm not going to have the answers you want to hear, but perhaps some of my explanations may provide some insight. First of all, CommonJS can be imported and used my ESM (as you are likely aware, note I'm trying to make my post appropriate for all readers, I'm not trying to be patronising or condescending). If you want to update your library to ESM, and still output CommonJS, unfortunately yes, you will need to use a build tool to transpile the ESM to CommonJS output. That's just how that works. I know, a bunch of you will tell me to "forget about CJS, it's the past", but in order to make a library consumable by the masses, you need to be able to support both. Most of the big libraries do this by literally outputting both an ESM output and a CJS output in the directory they publish to npm (using a build tool). Then, any users who consume that library can rely on their own build tools to remove the unused portion of the code (tree shaking). I'm sure I'll also get advice about building it in Typescript, but I've tried it and I'm not a fan (I would love syntax for types built into JavaScript, but one of the great things about JS is that it doesn't need to be compiled and, well, TS fundamentally changes that). I'm not going to tell you to use TypeScript. However, please allow me to clarify something here. If you don't switch to TypeScript, and you upgrade your codebase to ESM, then you're going to be introducing a tool to assist you in outputting a CommonJS output still. Currently for your code to support TypeScript, you need to manually create your own type declaration file (.d.ts), if you don't have one of these, then your library is not adequately consumable by those who use TypeScript. By using TypeScript, this declaration file will be generated for you. Additionally the tool that helps you do this is tsc - the official TypeScript transpiler, this is a tool that you need when using TypeScript, regardless of what output you're trying to achieve. With TypeScript and tsc alone, you can achieve an ESM TypeScript codebase which outputs both CommonJS and ESM formats, without any tooling beyond the native tooling required by TypeScript in the first place. I would imagine this is a better option than using some other third-party build tool to transpile your JavaScript how you desire. I REALLY want to make the switch, but without full backwards compatibility to do everything the same from a client-code perspective, there just doesn't seem to be a way around it. I'm going to have to do it (again) using CommonJS formatting and then make it better compatible with ESM imports. Any advice? If you really want to do something, and then you're deciding not to do it for X reasons (in this case, having to transpile), then you really don't want to do it. Because your desire to change is being trumped by your inability to adapt. Your resistance to change and your ability to adapt to these kinds of things is a character trait that is often measured throughout coding interviews throughout medium to big tech. There is nothing wrong with needing to use a tool to achieve this, as soon as you get over your distaste of using a tool, it all becomes extremely simple.
2 of 2
3
How are you going to support both CJS and ESM without compilation? I use rollup for this, but it does compile.
🌐
Reddit
reddit.com › r/node › importing with "import" vs "require"
r/node on Reddit: Importing with "Import" vs "Require"
August 12, 2024 -

I'm trying to import node-stream-zip and electron-dl into a node application. It seems like I can have all libraries imported with either import or require, but I can't pick and choose. I'm toggling between them in package.json by setting

    "type": "module"

If I set the type to module, I have to import everything with:

import {StreamZip} from "node-stream-zip"
import {download} from "electron-dl"

but this causes node-stream-zip to complain:

SyntaxError: Named export 'StreamZip' not found. The requested module 'node-stream-zip' is a CommonJS module, which may not support all module.exports as named exports.
CommonJS modules can always be imported via the default export, for example using:

import pkg from 'node-stream-zip';
const {StreamZip} = pkg;

Alternatively, I can import using:

const StreamZip = require('node-stream-zip');
const download = require("electron-dl")

but this causes `electron-dl` to complain:

Error [ERR_REQUIRE_ESM]: require() of ES Module [mypath]\electron-dl\index.js from [mypath]\main.js not supported. Instead change the require of index.js in [my_path]\main.js to a dynamic import() which is available in all CommonJS modules.

It seems strange to me that I can only use one import syntax or the other in a single main.js. Is this the case? If so, is there any way to either get node-stream-zip or electron-dl to be okay with the other import syntax?

🌐
GitHub
github.com › SBoudrias › Inquirer.js › issues › 1199
Error [ERR_REQUIRE_ESM]: require() of ES Module · Issue #1199 · SBoudrias/Inquirer.js
March 13, 2023 - ➜ npx ts-node src/index.ts I .../dist/index.js from /home/daniel/pro/gpt-cli/src/index.ts not supported. Instead change the require of index.js in /home/daniel/pro/gpt-cli/src/index.ts to a dynamic import() which is available ...
Author   gustawdaniel
🌐
GitHub
github.com › swc-project › swc › discussions › 8046
commonjs dynamic import esm not work · swc-project/swc · Discussion #8046
Instead change the require of index.js in /Users/pincman/Code/classroom/nestapp/dist/modules/content/controllers/category.controller.js to a dynamic import() which is available in all CommonJS modules.
Author   swc-project
🌐
Dmitri Pavlutin
dmitripavlutin.com › ecmascript-modules-dynamic-import
ES Modules Dynamic Import - Dmitri Pavlutin
March 22, 2023 - To load dynamically a module call import(path) as a function with an argument indicating the specifier (aka path) to a module. const module = await import(path) returns a promise that resolves to an object containing the components of the imported ...
Find elsewhere
🌐
GitHub
github.com › abhinaba-ghosh › playwright-lighthouse › issues › 72
lighthouse Playwright- Instead change the require of index.js, to a dynamic import() which is available in all CommonJS modules · Issue #72 · abhinaba-ghosh/playwright-lighthouse
February 14, 2024 - Scenario: Evaluate the performance ... from C:\Playwright\src\pages\example\exampleHomePage.ts not supported. Instead change the require of index.js in C:\Playwright\src\pages\example\exampleHomePage.ts to a dynamic import() which ...
Author   bachhavdipak
🌐
TechSparx
techsparx.com › nodejs › esnext › dynamic-import-2.html
Using Dynamic import in Node.js lets us import ES6 modules in CommonJS code, and more - UPDATED
January 21, 2020 - The Dynamic Import feature, a.k.a. the import() function, gives JavaScript programmers much more freedom over the standard import statement, plus it lets us import an ES6 module into CommonJS code.
🌐
GitHub
github.com › transitive-bullshit › agentic › issues › 80
Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules · Issue #80 · transitive-bullshit/agentic
December 10, 2022 - Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules#80
Author   badboy-tian
🌐
Medium
medium.com › @vviital › how-to-start-using-ecmascript-modules-in-a-real-life-node-js-project-c160d5a43da4
How to start using ECMAScript modules in a real Node.js project | by Vitali Zaneuski | Medium | Medium
January 15, 2023 - /Users/vitalizaneuski/.nvm/versions/node/v16.15.0/bin/node /Users/vitalizaneuski/projects/esm-test/broken-require.js /Users/vitalizaneuski/projects/esm-test/broken-require.js:3 const got = require("got"); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/vitalizaneuski/projects/esm-test/node_modules/got/dist/source/index.js from /Users/vitalizaneuski/projects/esm-test/broken-require.js not supported. Instead change the require of index.js in /Users/vitalizaneuski/projects/esm-test/broken-require.js to a dynamic import() which is available in all CommonJS modules.
🌐
GitHub
github.com › microsoft › playwright › issues › 17075
[BUG] dynamic import of ESM results in ERR_REQUIRE_ESM · Issue #17075 · microsoft/playwright
September 4, 2022 - From the node.js docs · Dynamic import() is supported in both CommonJS and ES modules. In CommonJS modules it can be used to load ES modules
Author   Janpot
🌐
GitHub
github.com › ant-design › ant-design › issues › 40644
[BUG] Error [ERR_REQUIRE_ESM]: require() of ES Module. dynamic import() which is available in all CommonJS modules. · Issue #40644 · ant-design/ant-design
February 8, 2023 - Error [ERR_REQUIRE_ESM]: require() of ES Module \remix-ant\node_modules\d3-interpolate\src\index.js from \remix-ant\node_modules@antv\g-base\lib\animate\timeline.js not supported. Instead change the require of index.js in \remix-ant\node_mo...
Author   EvgeniyBudaev
🌐
Medium
thedrlambda.medium.com › nodejs-typescript-and-the-infuriating-esm-errors-828b77e7ecd3
NodeJS, Typescript, and the infuriating ESM errors | by Christian Clausen | Medium
March 26, 2026 - So when you add your first ESM dependency you get: Error [ERR_REQUIRE_ESM]: require() of ES Module C:\XXXX.js not supported. Instead change the require of XXXX.js in C:\XXXX.js to a dynamic import() which is available in all CommonJS modules.
🌐
Sanity
sanity.io › exchange › answers › dynamic import() es module error...
Dynamic import() ES module error in Node.js prebuild script
November 29, 2025 - You're correct that Node.js supports dynamic import() in CommonJS files - but there's a critical detail: the file you're importing (../config.js) must be recognized as an ES module by Node.js.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-error-err-require-esm-require-of-es-module-not-supported
Error [ERR_REQUIRE_ESM]: require() of ES Module not supported | bobbyhadz
March 6, 2024 - Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to an ESM-only package, which means that the package cannot be imported with ...