Setting Client commands in ES Module
I am trying to set commands in an ESModule:
And getting this error:
This is what the
Is there any issue with using
const __dirname = "./";
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
await Promise.all(
commandFiles.map(async (file) => {
const filePath = path.join(commandsPath, file);
const { default: command } = await import(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
})
);
}const __dirname = "./";
const foldersPath = path.join(__dirname, "commands");
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs
.readdirSync(commandsPath)
.filter((file) => file.endsWith(".js"));
await Promise.all(
commandFiles.map(async (file) => {
const filePath = path.join(commandsPath, file);
const { default: command } = await import(filePath);
// Set a new item in the Collection with the key as the command name and the value as the exported module
if ("data" in command && "execute" in command) {
client.commands.set(command.data.name, command);
} else {
console.log(
`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`
);
}
})
);
}And getting this error:
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "commands\utility\convert.js" is not a valid package name imported from S:\NBV2\index.js
...
code: 'ERR_INVALID_MODULE_SPECIFIER'TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "commands\utility\convert.js" is not a valid package name imported from S:\NBV2\index.js
...
code: 'ERR_INVALID_MODULE_SPECIFIER'This is what the
./commands/utility/convert.js./commands/utility/convert.js looks like:import { SlashCommandBuilder } from "discord.js";
import { getInjectiveAddress, getEthereumAddress } from "@injectivelabs/sdk-ts";
import { codeblock } from "../../utils/utilities";
const data = new SlashCommandBuilder()
.setName("convert")
.setDescription("Convert address to INJ <> EVM")
...
async function execute(interaction) {...}
export {data, execute}import { SlashCommandBuilder } from "discord.js";
import { getInjectiveAddress, getEthereumAddress } from "@injectivelabs/sdk-ts";
import { codeblock } from "../../utils/utilities";
const data = new SlashCommandBuilder()
.setName("convert")
.setDescription("Convert address to INJ <> EVM")
...
async function execute(interaction) {...}
export {data, execute}Is there any issue with using
const { default: command } = await import(filePath);const { default: command } = await import(filePath); or is something else the problem? Thank you!