NodeJS 找不到模块“./src/命令/工具/ping. js”

crcmnpdw  于 2023-02-08  发布在  Node.js
关注(0)|答案(1)|浏览(136)

每次我尝试运行我的不和谐机器人我得到这个错误:

node:internal/modules/cjs/loader:1047
  const err = new Error(message);
              ^

Error: Cannot find module './src/Commands/Tools/ping.js'
Require stack:
- /Users/011935/Programming/JS/Bots/Command Bot v3/src/Functions/Handlers/commandHandler.js
- /Users/011935/Programming/JS/Bots/Command Bot v3/src/bot.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1047:15)
    at Module._load (node:internal/modules/cjs/loader:893:27)
    at Module.require (node:internal/modules/cjs/loader:1113:19)
    at require (node:internal/modules/cjs/helpers:103:18)
    at client.handleCommands (/Users/011935/Programming/JS/Bots/Command Bot v3/src/Functions/Handlers/commandHandler.js:15:25)
    at Object.<anonymous> (/Users/011935/Programming/JS/Bots/Command Bot v3/src/bot.js:19:8)
    at Module._compile (node:internal/modules/cjs/loader:1226:14)
    at Module._extensions..js (node:internal/modules/cjs/loader:1280:10)
    at Module.load (node:internal/modules/cjs/loader:1089:32)
    at Module._load (node:internal/modules/cjs/loader:930:12) {
  code: 'MODULE_NOT_FOUND',
  requireStack: [
    '/Users/011935/Programming/JS/Bots/Command Bot v3/src/Functions/Handlers/commandHandler.js',
    '/Users/011935/Programming/JS/Bots/Command Bot v3/src/bot.js'
  ]
}

下面是我的bot.js、commandHandler.js和ping.js文件以及文件结构
bot.js

require("dotenv").config();
const { token } = process.env;
const { Client, Collection, GatewayIntentBits } = require("discord.js");
const fs = require("fs");

const client = new Client({ intents: GatewayIntentBits.Guilds });
client.commands = new Collection();
client.commandArray = [];

const functionFolders = fs.readdirSync(`./src/Functions`);
for (const folder of functionFolders) {
  const functionFiles = fs
    .readdirSync(`./src/Functions/${folder}`)
    .filter((file) => file.endsWith(".js"));
  for (const file of functionFiles)
    require(`./Functions/${folder}/${file}`)(client);
}

client.handleCommands();
client.handleEvents();

client.login(token);

commandHandler.js

const { REST } = require("@discordjs/rest");
const { Routes } = require("discord-api-types/v9");
const fs = require("fs");
const path = require("node:path");

module.exports = (client) => {
  client.handleCommands = async () => {
    const commandFolders = fs.readdirSync(`./src/Commands`);
    for (const folder of commandFolders) {
      const commandFiles = fs
        .readdirSync(`src/Commands/${folder}`)
        .filter((file) => file.endsWith(".js"));

      const { commands, commandArray } = client;
      for (const file of commandFiles) {
        const command = require(`src/Commands/${folder}/${file}`);
        commands.set(command.data.name, command);
        commandArray.push(command, command.data.toJSON());
        console.log(`Command: ${command.data.name} has been registered`);
      }
    }

    const clientId = "1070133880671174697";
    const guildId = "1070126560004276305";
    const rest = new REST({ version: "9" }).setToken(process.env.token);

    try {
      console.log("Started refreshing commands.");

      await rest.put(Routes.applicationCommands(clientId, guildId), {
        body: client.commandArray,
      });

      console.log("Successfully refreshed commands.");
    } catch (err) {
      console.error(error);
    }
  };
};

ping.js

module.exports = {
  data: new SlashCommandBuilder()
    .setName("ping")
    .setDescription("Returns my ping"),
  async execute(interaction, client) {
    console.log("pinged");
  },
};

文件结构
File Structure
我试着自己编辑它,检查我在教程中写的代码是否正确,并广泛地搜索,但没有任何效果,对于那些有同样问题的人来说,他们只是"神奇地"修复了。
编辑1-我已经尝试了@rebe100x建议的../../src,但是当我使用path. basename时,它给了我这个错误

Error: ENOENT: no such file or directory, scandir 'Tools'

编辑2-我使用了"src/Commands/${folder}",但现在它给出了与以前相同的错误

g6ll5ycj

g6ll5ycj1#

在commandHandler.js的第15行

const command = require(`./src/Commands/${folder}/${file}`);

文件目录错误。从您的结构中,commandHandler.js位于Function > Handlers > commandHandler.js中。
只需将目录更改为../../src/Commands/${folder}/${file},就可以修复路径问题

相关问题