首页 > 解决方案 > 尝试制作 Discord 机器人,但找不到模块

问题描述

图片描绘了我的 index.js 属性,我一直在尝试重新编码我之前丢失的 Discord 机器人。我按照这个教程进行了操作,我之前已经完成了这个教程来制作一个不同的机器人。它就像一个魅力。我正处于运行命令npm run dev启动机器人的位置。预期结果:discord bot 启动成功实际结果:

PS C:\Users\User\Desktop\beans> npm run dev

┌───────────────────────────────────────────────────────────┐
│                  npm update check failed                  │
│            Try running with sudo or get access            │
│           to the local update config store via            │
│ sudo chown -R $USER:$(id -gn $USER) C:\Users\User\.config │
└───────────────────────────────────────────────────────────┘

> beans@1.0.0 dev C:\Users\User\Desktop\beans
> nodemon index.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js index.js`
internal/modules/cjs/loader.js:969
  throw err;
  ^

Error: Cannot find module 'C:\Users\User\Desktop\beans\index.js'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:966:15)
    at Function.Module._load (internal/modules/cjs/loader.js:842:27)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:71:12)
    at internal/main/run_main_module.js:17:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}
[nodemon] app crashed - waiting for file changes before starting...

那么,这是我的 package.json:

{
  "name": "beans",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node index.js",
    "dev": "nodemon index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "discord.js": "^12.2.0"
  },
  "devDependencies": {
    "nodemon": "^2.0.4"
  }
}

这是我的 index.js,减去 bot 令牌:

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("mytoken")

我试过跑步npm clear cache,,但什么也没做。我已经尝试完全重新创建文件,也没有做任何事情。如果有帮助,我在 Windows 10 上。这是我的第一个问题,所以如果我没有提供足够的信息,我深表歉意,但我希望这能清楚地表达出来。

编辑:我注意到,与我上次启动不同的机器人相比,终端/powershell 状态starting node index.js index.js而不是starting node index.js上次那样。我想我可能会看到错误,但我不知道如何修复它。

标签: javascriptnode.jsnpmdiscord.js

解决方案


我运行您的代码,结果如下。令牌问题在意料之中,但它确实找到了模块

C:\git\beans
> npm run dev

> beans@1.0.0 dev C:\git\beans
> nodemon index.js

[nodemon] 2.0.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,json
[nodemon] starting `node index.js`
(node:47012) UnhandledPromiseRejectionWarning: Error [TOKEN_INVALID]: An invalid token was provided.

推荐阅读