使用 REPLIT 创建我的第一个机器人,但总是出现错误 Discord.JS

Men*_*ndo 4 javascript node.js discord discord.js

我刚刚在 Replit 上启动了 Discord.JS。它遵循https://www.freecodecamp.org/news/create-a-discord-bot-with-javascript-nodejs/上的 FreeCodeCamp 教程

但是当我运行代码时它告诉我运行:

const Discord = require("discord.js")
const client = new Discord.Client()

client.on("ready", () => {
  console.log(`Logged in as ${client.user.tag}!`)
})

client.on("message", msg => {
  if (msg.content === "ping") {
    msg.reply("pong");
  }
})

client.login(process.env.TOKEN)
Run Code Online (Sandbox Code Playgroud)

它告诉这个错误:

Error: Cannot find module 'node:events'
Require stack:
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/Omega-Flowey-JS-Test/node_modules/discor
Error: Cannot find module 'node:events'
Require stack:
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/client/BaseClient.js
- /home/runner/Omega-Flowey-JS-Test/node_modules/discord.js/src/index.js
- /home/runner/Omega-Flowey-JS-Test/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/Omega-Flowey-JS-Test/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
Run Code Online (Sandbox Code Playgroud)

有人能告诉我什么错误吗?我是 Discord.JS 的新手,所以我不知道有什么错误。

che*_*som 6

Discord.js v13 需要 Node.js v16.6。Replit 仅直接支持 Node.js v12.16.1(在撰写本文时)。

使用旧版本的 Node.js 时,您可能会看到以下错误:

但是,您可以使用Node npm 包安装较新的 Node.js 二进制文件。

这是一个最小的设置:

package.json

{
  "name": "node.js-v16-on-replit-demo",
  "version": "0.0.1",
  "dependencies": {
    "discord.js": "^13.2.0",
    "node": "^16.10.0"
  }
}
Run Code Online (Sandbox Code Playgroud)

.replit

run = "npx node ."
Run Code Online (Sandbox Code Playgroud)

index.js

run = "npx node ."
Run Code Online (Sandbox Code Playgroud)

您可以通过此演示 repl尝试一下。

如果您愿意,您可以定义一个startnpm 脚本:

package.json

const Discord = require("discord.js")

// your code...
Run Code Online (Sandbox Code Playgroud)

.replit

run = 'npm start'
Run Code Online (Sandbox Code Playgroud)

Replit 上还有大量有关 Node.js v16 的在线资源。