Discord.js: Error: "Cannot find module 'node:events'" after updating to v13

Discord.js: Error: "Cannot find module 'node:events'" after updating to v13

今天我将我的节点版本从 12.x 更新到 17.1.0,将我的 discord.js 更新到 13.3.1。更新过程成功,但在我 运行 我的机器人之后,它输出错误:

Error: Cannot find module 'node:events'
Require stack:
- /home/runner/rice-v13/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/rice-v13/node_modules/discord.js/src/index.js
- /home/runner/rice-v13/index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:815:15)
    at Function.Module._load (internal/modules/cjs/loader.js:667:27)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/runner/rice-v13/node_modules/discord.js/src/client/BaseClient.js:3:22)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19

此外,我注意到当我通过 shell 安装新的节点版本时它会输出警告:

npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.13 (node_modules/fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.13: wanted {"os":"darwin","arch":"any"} (current: {"os":"linux","arch":"x64"})

我在 Internet 上浏览了这个问题,只找到了类似“您必须拥有 16 或更高版本的节点”之类的答案,但实际上我有 17.1.0。

我看到您正在为您的机器人使用 replit。 当您的 repl 节点版本不够高时,会发生此错误。 您可以使用 NodeJS v16 模板 (https://replit.com/@RoBlockHead/NodeJS-16) 修复该问题。只需 fork 那个 repl 模板并将所有代码移到那里。

而不是:

const client = new Discord.Client();

类型:

const client = new Discord.Client({ intents: ["DIRECT_MESSAGES", "GUILD_MESSAGES"] });