26

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

ProGamer2711
  • 451
  • 1
  • 5
  • 18

4 Answers4

32

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.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
14

With the latest update, node-fetch only works by using import

You could just install the older version of it by npm i node-fetch@2.6.1

Azer
  • 446
  • 2
  • 8
5

For those coming here trying to deploy a NodeJS App using the cPanel feature, remember that under the hood it uses Phusion Passenger. This tool needs to have a non-module entry point like:

// entry.cjs DONT FORGET TO USE .cjs and not .js

async function loadApp() {
    const { app } = await import("./app.js"); // this is your normal entry file - (index.js, main.js, app.mjs etc.)
}
loadApp()

The original answer is here (it deserves your vote): https://stackoverflow.com/a/71901828/14515077

Nico Serrano
  • 577
  • 4
  • 14
  • 1
    I spent hours looking for solution, only yours works! Thanks a lot! Is it a problem of the old version of phusion or is it still there? – Alexander Petrushyn Jul 18 '23 at 16:32
  • I havent tried to deploy something with cPanel in ages but I think that Pushion would someday accept module file as entry points – Nico Serrano Jul 20 '23 at 06:42
0

It will work if you use an older version of the node-fetch module and then use the npm install command. I solved it this way

EmreG
  • 19
  • 2