Converting the Command Handler and the Command Deployer to use ES modules

I am attempting to convert my project to an ES Module instead of using CommonJS. I've already changed imports from require() to import, and i've changed the package.json to include "type": "module". The problem is changing the command handler and command deployer to work, since both (taken directly from the guide) were designed to work using CommonJS. djs version: 14.14.1 node version: 18.10.0 The first issue is that __dirname is not defined. There are also issues with joining paths. Any suggestions on how to best modify the command handler to work with ES Modules would be greatly appreciated. Code will be included in my next message because of character limits
6 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! - Marked as resolved by OP
ales
alesOP11mo ago
Command Handler in index.js:
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] })

client.commands = new Collection()

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'))
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file)
const command = require(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.`
)
}
}
}
// Create a new client instance
const client = new Client({ intents: [GatewayIntentBits.Guilds] })

client.commands = new Collection()

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'))
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file)
const command = require(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.`
)
}
}
}
im having issues sending the command-handler.js code but it's the exact same as the guide, I just changed the imports thank you!
Superchupu
Superchupu11mo ago
@ales actually if you use node versions over 21.2.0 you can use import.meta.dirname/import.meta.filename instead, they got added recently https://nodejs.org/api/esm.html#importmetafilename
ales
alesOP11mo ago
oh ok, maybe i'll update then
Superchupu
Superchupu11mo ago
it's a drop in replacement of __dirname and __filename iirc
ales
alesOP11mo ago
i'll go with that then thank you!
Want results from more Discord servers?
Add your server