【问题标题】:Can't install discord.js / can't find module discord.js无法安装 discord.js / 找不到模块 discord.js
【发布时间】:2020-07-06 16:25:23
【问题描述】:

我尝试通过运行npm install discord.js 来安装 Discord.JS,它看起来可以正常工作,但实际上并没有。

我在运行index.js 文件时收到此错误,但它给出了一个错误,指出找不到discord.js。所以,我尝试再次安装它:

PS G:\My Drive\coding\node.js\bot> npm install https://github.com/discordjs/discord.js.git
npm notice created a lockfile as package-lock.json. You should commit this file.
npm WARN bot@0.0.1 No repository field.

+ discord.js@12.0.2
added 15 packages from 17 contributors and audited 15 packages in 97.377s
found 0 vulnerabilities

然后我运行index.js 文件:

const Discord = require('discord.js');
const bot = new Discord.Client();
const botCommands = require('./commands');
const { prefix, token } = require('./cfg.json');

bot.login(TOKEN);

bot.on('ready', () => {
    console.info(`Logged in as ${bot.user.tag}!`);
});

这是控制台中显示的结果:

PS G:\My Drive\coding\node.js\bot> node .
internal/modules/cjs/loader.js:796
    throw err;
    ^

Error: Cannot find module './commands'
Require stack:
- G:\My Drive\coding\node.js\bot\index.js
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:793:17)
    at Function.Module._load (internal/modules/cjs/loader.js:686:27)
    at Module.require (internal/modules/cjs/loader.js:848:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (G:\My Drive\coding\node.js\bot\index.js:3:21)
    at Module._compile (internal/modules/cjs/loader.js:955:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)
    at Module.load (internal/modules/cjs/loader.js:811:32)
    at Function.Module._load (internal/modules/cjs/loader.js:723:14)
    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [ 'G:\\My Drive\\coding\\node.js\\bot\\index.js' ]
}

旁注:在我运行index.js 之前,我运行npm init -y 来创建一个包。

P.S: 英语不是我的母语

【问题讨论】:

  • Error: Cannot find module './commands'
  • 是的,我的错。真的很抱歉。
  • 我认为第一次 discord.js 安装失败,我多次运行该文件,结果不同,我没有注意到。结案。谢谢。

标签: javascript node.js npm discord.js


【解决方案1】:

如果您找不到 /commands 文件夹 - 您确定它在那里吗? - 确保它在文件夹'G:\My Drive\coding\node.js\bot\'中。

【讨论】:

  • 这不是它要搜索的命令文件夹。当需要一个没有扩展名的文件时,节点会自动将.js附加到它,所以它实际上是在搜索commands.js。
  • 代码不是我写的。我只是说明他写的代码是做什么的。
  • 其实哈哈,首先,谢谢。但是这个问题很久以前就解决了。实际上问题是我认为访问 discord.js 有问题。现在已经结案了。下一个问题我也解决了。只是fs.readdirSync('./commands').filter(file =&gt; file.endsWith('.js')); 而且,它运行良好。整个问题已结案。问这个问题只是我很愚蠢。
【解决方案2】:

好的。首先,它说没有找到模块“./commands”,discord.js确实存在。

原因是,您正在尝试导入文件夹。 Node.js 不能那样工作。

您必须删除带有 require("./commands") 的行并将其替换为以下内容:

var botCommands = fs.readdirSync('./commands/');

这将返回该目录中的文件名数组。

然后,继续你正在做的事情。

【讨论】:

    【解决方案3】:

    我认为你想要的是类似的东西

    bot.on('message', message => {
    
      if(message.content.startsWith(prefix)) {
         let rawA = message.content.slice(prefix.length).split(' ');
         let cmd = rawA[0];
         let rawB = rawA.join(' ');
         let args = rawB.slice(cmd.length).split(' ');
      
         let cmdFile = require(`./commands/${cmd}.js`);
         cmdFile.run(bot, message, args);
      }
    }
    

    但是如果你使用这个,你需要在你的命令文件中使用这个:

    exports.run = async (bot, message, args) => {
       //code
    }
    

    【讨论】:

      【解决方案4】:

      错误出现在您需要命令的第 3 行

      如果您有命令文件夹,请执行此操作

      fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-06
        • 2019-01-18
        • 2019-10-11
        • 2021-10-05
        • 1970-01-01
        • 2020-09-10
        • 2020-02-22
        • 2023-01-13
        相关资源
        最近更新 更多