Setting Client commands in ES Module

I am trying to set commands in an ESModule:
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 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); or is something else the problem? Thank you!
4 Replies
d.js toolkit
d.js toolkit11mo ago
- What's your exact discord.js npm list discord.js and node node -v version? - Not a discord.js issue? Check out #other-js-ts. - Consider reading #how-to-get-help to improve your question! - Explain what exactly your issue is. - Post the full error stack trace, not just the top part! - Show your code! - Issue solved? Press the button!
Lahn ᓚᘏᗢ
Lahn ᓚᘏᗢOP11mo ago
TypeError [ERR_INVALID_MODULE_SPECIFIER]: Invalid module "commands\utility\convert.js" is not a valid package name imported from S:\NBV2\index.js
at new NodeError (node:internal/errors:405:5)
at parsePackageName (node:internal/modules/esm/resolve:818:11)
at packageResolve (node:internal/modules/esm/resolve:841:5)
at moduleResolve (node:internal/modules/esm/resolve:939:20)
at defaultResolve (node:internal/modules/esm/resolve:1132:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ESMLoader.import (node:internal/modules/esm/loader:524:22)
at importModuleDynamically (node:internal/modules/esm/translators:110:35) {
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
at new NodeError (node:internal/errors:405:5)
at parsePackageName (node:internal/modules/esm/resolve:818:11)
at packageResolve (node:internal/modules/esm/resolve:841:5)
at moduleResolve (node:internal/modules/esm/resolve:939:20)
at defaultResolve (node:internal/modules/esm/resolve:1132:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:835:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ESMLoader.import (node:internal/modules/esm/loader:524:22)
at importModuleDynamically (node:internal/modules/esm/translators:110:35) {
code: 'ERR_INVALID_MODULE_SPECIFIER'
}
axiprime2.0
axiprime2.011mo ago
import { codeblock } from "../../utils/utilities"; you are not importing correctly the file.It missing the extention, like .js
Lahn ᓚᘏᗢ
Lahn ᓚᘏᗢOP11mo ago
I changed it to import { codeblock } from "../../utils/utilities.js"; but still getting same error. tysm!
Want results from more Discord servers?
Add your server