简体   繁体   中英

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM