You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行node deploy-commands.js注册Discord命令时遇模块未找到错误

执行node deploy-commands.js时出现MODULE_NOT_FOUND错误(discord.js命令注册)

问题详情

在discord.js项目中尝试注册Slash命令,已确认deploy-commands.js代码与拼写无误,但执行node deploy-commands.js时返回以下错误:

Error: Cannot find module 'C:\Program Files\Microsoft Visual Studio\2022\Preview\deploy-commands.js'
    at Module._resolveFilename (node:internal/modules/cjs/loader:1077:15)
    at Module._load (node:internal/modules/cjs/loader:922:27)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:23:47 {
  code: 'MODULE_NOT_FOUND',
  requireStack: []
}

Node.js v18.16.1

使用的deploy-commands.js代码如下:

const { REST, Routes } = require('discord.js');
const { clientId, guildId, token } = require('./config.json');
const fs = require('node:fs');
const path = require('node:path');

const commands = [];
// Grab all the command files from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders) {
    // Grab all the command files from the commands directory you created earlier
    const commandsPath = path.join(foldersPath, folder);
    const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
    // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
    for (const file of commandFiles) {
        const filePath = path.join(commandsPath, file);
        const command = require(filePath);
        if ('data' in command && 'execute' in command) {
            commands.push(command.data.toJSON());
        } else {
            console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
        }
    }
}

// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);

// and deploy your commands!
(async () => {
    try {
        console.log(`Started refreshing ${commands.length} application (/) commands.`);

        // The put method is used to fully refresh all commands in the guild with the current set
        const data = await rest.put(
            Routes.applicationGuildCommands(clientId, guildId),
            { body: commands },
        );

        console.log(`Successfully reloaded ${data.length} application (/) commands.`);
    } catch (error) {
        // And of course, make sure you catch and log any errors!
        console.error(error);
    }
})();

解决方案

1. 切换到正确的工作目录

错误提示的路径是VS安装目录,说明当前终端工作目录并非你的discord.js项目根目录:

  • 打开终端,用cd命令切换到项目根目录(即存放deploy-commands.js、config.json的文件夹),例如:
    cd C:\Users\YourName\Documents\DiscordBotProject
    
  • 执行dir(Windows)或ls(Mac/Linux)确认当前目录存在目标文件,再重新执行node deploy-commands.js。

2. 检查文件名拼写与大小写

确认项目根目录下的文件确实是deploy-commands.js:

  • 核对连字符、拼写是否正确(避免deploycommands.js或deploy-command.js这类错误);
  • Mac/Linux系统需注意大小写敏感,确保命令中的文件名与实际完全一致。

3. 使用绝对路径执行(备选)

若不想切换目录,可直接用文件绝对路径执行命令,路径含空格时需用双引号包裹:

node "C:\Users\YourName\Documents\DiscordBotProject\deploy-commands.js"

内容的提问来源于stack exchange,提问作者Moose

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.16 22:42:47