Discord.js报错:找不到模块'./commands/info/status.js'求解决
Discord.js机器人报错排查:Cannot find module './commands/info/status.js'
问题描述
开发Discord.js机器人时触发以下错误:
node:internal/modules/cjs/loader:936 throw err; ^ Error: Cannot find module './commands/info/status.js' Require stack: C:\Users\satang\Desktop\bot_node_satang\dis3\handler\command.js C:\Users\satang\Desktop\bot_node_satang\dis3\index.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15) at Function.Module._load (node:internal/modules/cjs/loader:778:27) at Module.require (node:internal/modules/cjs/loader:1005:19) at require (node:internal/modules/cjs/helpers:94:18) at C:\Users\satang\Desktop\bot_node_satang\dis3\handler\command.js:15:26 at Array.forEach () at module.exports (C:\Users\satang\Desktop\bot_node_satang\dis3\handler\command.js:12:32) at C:\Users\satang\Desktop\bot_node_satang\dis3\index.js:68:34 at Array.forEach () at Object. (C:\Users\satang\Desktop\bot_node_satang\dis3\index.js:67:13) { code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\Users\satang\Desktop\bot_node_satang\dis3\handler\command.js', 'C:\Users\satang\Desktop\bot_node_satang\dis3\index.js' ] }
相关代码文件
命令处理器 handler/command.js
const { readdirSync } = require('fs'); const ascii = require('ascii-table'); const client = require('..'); const { dir } = require('console'); const { config } = require('process'); let table = new ascii("Commands"); table.setHeading('Commands', 'Load'); module.exports = (client) => { readdirSync('./commands/').forEach(dir => { const commands = readdirSync(`./commands/${dir}/`).filter(file => file.endsWith(".js")); for(const file of commands) { const pull = require(`./commands/${dir}/${file}`); if (pull.name) { client.commands.set(pull.name, pull); slash.push(pull); table.addRow(file, "OK"); }else { table.addRow(file, 'Error - Missing a help.name or it is not a string...'); continue; }if(pull.aliases && Array.isArray(pull.aliases)) pull.aliases.forEach(alias => client.aliases.set(alias, pull.name)) } }); console.log(table.toString()); readdirSync('./events/').forEach((file) => { const events = readdirSync('./events').filter((file) => file.endsWith('.js') ); for (let file of events) { let pull = require(`../events/${file}`); if(pull.name) { client.events.set(pull.name, pull) } } console.log(`${file} Events load`); }) }
事件文件 events/messageCreate.js
const { Embed, Collection } = require('discord.js') const client = require('..'); const config = require('../setting/config.json'); const prefix_bot = config.prefix_bot; client.on("messageCreste", async (message) => { if(message.author.bot) return; if(!message.content.startsWith(prefix_bot)) return; if(!message.guild) return; if(!message.member) message.member = await message.guild.fetchMember(message); const args = message.content.slice(prefix_bot.length).trim().split(/ +/g); const cmd = args.shift().toLowerCase(); if(cmd.length == 0) return; let command = client.commands.get(cmd); if(!command) command = client.commands.get(client.aliases.get(cmd)); if(command) command.run(client, message, args) });
命令文件 commands/info/status.js
const { Embed, Collection } = require('discord.js') const client = require('..'); const config = require('../setting/config.json'); const prefix_bot = config.prefix_bot; client.on("messageCreste", async (message) => { if(message.author.bot) return; if(!message.content.startsWith(prefix_bot)) return; if(!message.guild) return; if(!message.member) message.member = await message.guild.fetchMember(message); const args = message.content.slice(prefix_bot.length).trim().split(/ +/g); const cmd = args.shift().toLowerCase(); if(cmd.length == 0) return; let command = client.commands.get(cmd); if(!command) command = client.commands.get(client.aliases.get(cmd)); if(command) command.run(client, message, args) });
错误分析与解决方案
1. 核心问题:相对路径错误
command.js位于handler文件夹下,代码中使用./commands/${dir}/${file}是相对于handler目录的路径,但实际commands目录在项目根目录(与handler同级),因此路径需要向上跳转一级。
修复方式:
将command.js中第15行的:
const pull = require(`./commands/${dir}/${file}`);
修改为:
const pull = require(`../commands/${dir}/${file}`);
同时确认:
commands/info/status.js文件确实存在,文件名无拼写错误(注意Linux/macOS系统区分文件名大小写)- 目录结构正确:
dis3/commands/info/status.js
2. status.js文件内容错误
当前status.js的内容完全复制了messageCreate.js的事件逻辑,不符合命令文件规范。命令文件需要导出包含name和run方法的对象,供命令处理器加载。
修复方式:
重新编写status.js为标准命令结构(以Discord.js v14为例):
const { EmbedBuilder } = require('discord.js'); const config = require('../../setting/config.json'); // 根据实际路径调整 module.exports = { name: 'status', // 命令名称,需与处理器匹配 aliases: ['stats'], // 可选命令别名 run: async (client, message, args) => { // 示例:发送机器人状态嵌入 const statusEmbed = new EmbedBuilder() .setTitle('机器人状态') .addFields( { name: '服务数量', value: `${client.guilds.cache.size}`, inline: true }, { name: '用户数量', value: `${client.users.cache.size}`, inline: true }, { name: '前缀', value: `${config.prefix_bot}`, inline: true } ) .setColor('#0099ff'); await message.reply({ embeds: [statusEmbed] }); } };
3. 其他次要问题修复
- 事件名拼写错误:messageCreate.js中
messageCreste应为messageCreate,否则事件无法触发 - 未定义变量:command.js中
slash变量未声明,需在开头添加const slash = []; - 事件加载逻辑冗余:优化command.js的事件加载代码,避免重复读取目录:
// 替换原事件加载代码 const eventFiles = readdirSync('./events').filter(file => file.endsWith('.js')); for (const file of eventFiles) { const pull = require(`../events/${file}`); if (pull.name && pull.execute) { client.events.set(pull.name, pull); client.on(pull.name, pull.execute.bind(null, client)); } console.log(`${file} 事件加载完成`); }
内容的提问来源于stack exchange,提问作者Tussapon Tammai
相关产品推荐
相关产品推荐

