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 limits6 Replies
- 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 OPCommand Handler in index.js:
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!
@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#importmetafilenameoh ok, maybe i'll update then
it's a drop in replacement of __dirname and __filename iirc
i'll go with that then
thank you!