16

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

5

4 Answers 4

23

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.

10

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

1

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

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

1
  • You know this has been solved before, right? Mar 8 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.