[英]Error [ERR_REQUIRE_ESM]: require() of ES Module ... not supported
I'm making a discord bot in TypeScript
using discord.js
.我正在使用
discord.js
在TypeScript
中制作 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:您有以下选择需要修复:
Switch your project to an ESM module and load it with import fetch from 'node-fetch';
将您的项目切换到 ESM 模块并使用
import fetch from 'node-fetch';
. .
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 模块。
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.