【问题标题】:Trying to make a Discord bot, but module cannot be found尝试制作 Discord 机器人,但找不到模块
【发布时间】:2020-11-02 03:38:51
【问题描述】:

Image depicts my index.js properties 我一直在尝试重新编码我之前丢失的 Discord 机器人。我按照THIS 教程点了点,我以前做过这个教程来制作一个不同的机器人。它就像一个魅力。我正在运行命令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,减去机器人令牌:

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。我想我可能会看到错误,但我不知道如何修复它。

【问题讨论】:

    标签: javascript node.js npm discord.js


    【解决方案1】:

    误报...我的 index.js 决定保存为 index.js.txt,尽管只显示 index.js。我切换了记事本程序,它就像一个魅力。

    【讨论】:

    • 很高兴你能成功。如果您还没有,您可能希望为您的 js 尝试 Visual Studio 代码。 code.visualstudio.com 另外我建议打开此功能:文件资源管理器 > 视图 > 文件扩展名,以便扩展名始终显示在 Windows 中。
    【解决方案2】:

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

    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.
    

    【讨论】:

      【解决方案3】:

      可能是你的 NODE_PATH 环境变量搞砸了。

      尝试以下操作,看看是否有任何设置。 c:\git\beans

      设置 NODE_PATH

      【讨论】:

        【解决方案4】:

        错误提示找不到 index.js 可能是名称错误或目录错误。

        【讨论】:

        • 我倾向于同意这个帖子,你能发一张工作目录的照片吗?
        • Eep...我对此完全陌生,但我添加了图像。我希望这就是你的意思,如果不是,我很抱歉。
        • 我认为他们希望您使用命令 dir 列出工作目录的目录
        猜你喜欢
        • 2020-03-31
        • 2017-07-22
        • 1970-01-01
        • 2017-10-11
        • 2020-10-05
        • 1970-01-01
        • 2018-06-26
        • 2012-10-07
        • 2021-11-11
        相关资源
        最近更新 更多