Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm making a discord bot in TypeScript using discord.js. When I tried to compile code this morning I got this error:

C:\SECRET\Kostegator\dist\Util\getMeme.js:17
const node_fetch_1 = __importDefault(require("node-fetch"));
                                     ^

Error [ERR_REQUIRE_ESM]: require() of ES Module C:\SECRET\Kostegator\node_modules\node-fetch\src\index.js from C:\SECRET\Kostegator\dist\Util\getMeme.js not supported.
Instead change the require of index.js in C:\SECRET\Kostegator\dist\Util\getMeme.js to a dynamic import() which is available in all CommonJS modules.
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\Util\getMeme.js:17:38)
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\Util\index.js:15:14)
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\Commands\BotOwner\startAutoUpdate.js:4:16)
    at C:\SECRET\Kostegator\dist\Client\index.js:61:41
    at Array.forEach (<anonymous>)
    at ExtendedClient.<anonymous> (C:\SECRET\Kostegator\dist\Client\index.js:58:48)        
    at Generator.next (<anonymous>)
    at C:\SECRET\Kostegator\dist\Client\index.js:27:71
    at new Promise (<anonymous>)
    at __awaiter (C:\SECRET\Kostegator\dist\Client\index.js:23:12)
    at ExtendedClient.init (C:\SECRET\Kostegator\dist\Client\index.js:51:16)
    at Object.<anonymous> (C:\SECRET\Kostegator\dist\index.js:19:4) {
  code: 'ERR_REQUIRE_ESM'
}

Here's the GitHub repo: Kostegator

like image 447
ProGamer2711 Avatar asked Sep 01 '25 10:09

ProGamer2711


1 Answers

The current version of node-fetch is ONLY compatible with an ESM import (using import), not from CommonJS modules using require().

You have these choices to fix:

  1. Switch your project to an ESM module and load it with import fetch from 'node-fetch';.

  2. In a very recent version of nodejs, you can dynamically import an ESM module into a CommonJS module using let fetch = await import('node-fetch').

  3. Use the v2 version of node-fetch that still supports being loaded with require() as explained here in the doc.

like image 191
jfriend00 Avatar answered Sep 04 '25 03:09

jfriend00