【问题标题】:Creating my first bot using REPLIT but always error Discord.JS [duplicate]使用 REPLIT 创建我的第一个机器人但总是错误 Discord.JS [重复]
【发布时间】:2021-11-26 06:03:39
【问题描述】:

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

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

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)

它告诉这个错误:

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

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

【问题讨论】:

  • 您在运行npm install时是否遇到错误?
  • 我不知道,当我启动它时,它只是立即给出了那个错误。就像程序只是不想尝试运行并且只是给出错误一样

标签: javascript node.js discord discord.js


【解决方案1】:

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"
  }
}

.replit

run = "npx node ."

index.js

const Discord = require("discord.js")

// your code...

你可以试试这个demo repl

如果你愿意,你可以定义一个start npm 脚本:

package.json

{
  // ...
  "scripts": {
    "start": "node ."
  }
}

.replit

run = 'npm start'

Replit 上还有 plenty of resources online 关于 Node.js v16。

【讨论】:

  • 它可以工作,但是当我尝试输入任何类型的代码时,它只会出错
  • 你需要比“犯错误”更具体。请编辑您的问题以包含错误的完整详细信息,
  • 我只是想补充一点,即使使用v12也会发生这种情况,所以无论如何你都必须升级
  • 链接到“找不到模块 'node:events' 问题”:stackoverflow.com/q/69489101/15781079
猜你喜欢
  • 1970-01-01
  • 2018-01-08
  • 2020-05-27
  • 2021-05-16
  • 2021-11-10
  • 2020-06-30
  • 2021-01-31
  • 1970-01-01
  • 2021-10-26
相关资源
最近更新 更多