deploy commands folder

I'm trying to change the basic "deploy commands" that you give to us. I want to be able to read subfolder with files in the folder "commands"

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

const commandsPath = './commands';
const commands = [];

function readDirectory(dir) {
    const files = fs.readdirSync(dir);
    for (const file of files) {
      const filePath = path.join(dir, file);
      const stats = fs.statSync(filePath);
      if (stats.isFile() && file.endsWith('.js')) {
        try {
          const command = require(filePath);
          if ('data' in command && 'execute' in command) {
            commands.push(command.data.toJSON());
          } else {
            throw new Error(`Command at ${filePath} is missing required "data" or "execute" property.`);
          }
        } catch (error) {
          console.error(`[ERROR] Failed to load command from ${filePath}: ${error}`);
        }
      } else if (stats.isDirectory()) {
        readDirectory(filePath);
      }
    }
  }
  
readDirectory(commandsPath);
  
// Construct and prepare an instance of the REST module
const rest = new REST({ version: '10' }).setToken(token);

(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);
    }
})();


But it gives me that error :

[ERROR] Failed to load command from commands\ban\ban.js: Error: Cannot find module 'commands\ban\ban.js'
Require stack:
- c:\Users\virgi\OneDrive\Documents\Projets perso\progra\js\bots-altara\Thenix - Modération\deploy-commands.js
[ERROR] Failed to load command from commands\ban\unban.js: Error: Cannot find module 'commands\ban\unban.js'
Require stack:
- c:\Users\virgi\OneDrive\Documents\Projets perso\progra\js\bots-altara\Thenix - Modération\deploy-commands.js
[ERROR] Failed to load command from commands\kick.js: Error: Cannot find module 'commands\kick.js'
Require stack:
- c:\Users\virgi\OneDrive\Documents\Projets perso\progra\js\bots-altara\Thenix - Modération\deploy-commands.js
[ERROR] Failed to load command from commands\mute\mute.js: Error: Cannot find module 'commands\mute\mute.js'
Require stack:
- c:\Users\virgi\OneDrive\Documents\Projets perso\progra\js\bots-altara\Thenix - Modération\deploy-commands.js
[ERROR] Failed to load command from commands\mute\unmute.js: Error: Cannot find module 'commands\mute\unmute.js'
Require stack:
- c:\Users\virgi\OneDrive\Documents\Projets perso\progra\js\bots-altara\Thenix - Modération\deploy-commands.js
[ERROR] Failed to load command from commands\ping.js: Error: Cannot find module 'commands\ping.js'
Require stack:
- c:\Users\virgi\OneDrive\Documents\Projets perso\progra\js\bots-altara\Thenix - Modération\deploy-commands.js
Started refreshing 0 application (/) commands.
Successfully reloaded 0 application (/) commands.
`

I don't understand what's wrong.
Was this page helpful?