简体   繁体   English

错误 [ERR_REQUIRE_ESM]: ES 模块的 require()... 不支持

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

I'm making a discord bot in TypeScript using discord.js .我正在使用discord.jsTypeScript中制作 discord 机器人。 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这是 GitHub 存储库: Kostegator

The current version of node-fetch is ONLY compatible with an ESM import (using import ), not from CommonJS modules using require() .当前版本的node-fetch仅与 ESM 导入(使用import )兼容,而不与使用require()的 CommonJS 模块兼容。

You have these choices to fix:您有以下选择需要修复:

  1. Switch your project to an ESM module and load it with import fetch from 'node-fetch';将您的项目切换到 ESM 模块并使用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') .在最新版本的 nodejs 中,您可以使用let fetch = await import('node-fetch')将 ESM 模块动态导入 CommonJS 模块。

  3. Use the v2 version of node-fetch that still supports being loaded with require() as explained here in the doc.使用仍支持使用require()加载的 v2 版本node-fetch ,如文档中所述。

With the latest update, node-fetch only works by using import在最新更新中, node-fetch只能通过使用import

You could just install the older version of it by npm i node-fetch@2.6.1您可以通过npm i node-fetch@2.6.1安装旧版本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Vue 3 Typescript - Error [ERR_REQUIRE_ESM]: require() of ES Module - Vue 3 Typescript chalk - 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - chalk - Error [ERR_REQUIRE_ESM]: require() of ES Module 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module 错误 [ERR_REQUIRE_ESM]: 不支持 ES 模块 index.js 的 require() - Error [ERR_REQUIRE_ESM]: require() of ES Module index.js not supported 不支持 ES 模块的错误 [ERR_REQUIRE_ESM] require() Nuxt Js 3 - Error [ERR_REQUIRE_ESM] require() of ES Module not supported Nuxt Js 3 PM2 带 ES 模块。 错误:ERR_REQUIRE_ESM - PM2 with ES module. Error: ERR_REQUIRE_ESM const fetch = require("node-fetch"); ^ 错误 [ERR_REQUIRE_ESM]:ES 模块的 require() - const fetch = require("node-fetch"); ^ Error [ERR_REQUIRE_ESM]: require() of ES Module 错误 [ERR_REQUIRE_ESM]: 使用 SSR (Inertiajs) 时需要 ES 模块 - Error [ERR_REQUIRE_ESM]: require() of ES Module while using SSR (Inertiajs) 错误 [ERR_REQUIRE_ESM]: 运行脚本时必须使用 import 加载 ES 模块 - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module when running script 错误 [ERR_REQUIRE_ESM]:必须使用导入来加载 ES 模块: - Error [ERR_REQUIRE_ESM]: Must use import to load ES Module:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM