Home > Blockchain >  I want to import all command files from a folder and execute them in ES6
I want to import all command files from a folder and execute them in ES6

Time:05-23

In my guild command file I have to require all files of the commands folder and save them as a variable in a FOR-Loop.

In ES5 it would look like that:

for (const file of commandFiles) {
   const command = require(`../commands/${file}`);
   commands.push(command.data.toJSON());
}

But I want to import everything with ES6 and the import statement. Is there a way to convert the code above to ES6-compatible code?

CodePudding user response:

for (const file of commandFiles) {
    const command = await import(`../commands/${file}`);
    commands.push(command.data.toJSON());
}

CodePudding user response:

Try something like this:

  for (const file of commandFiles) {
        const command = require(`./commands/${file}`);
        client.commands.set(command.name, command);
    }
  • Related