Mixing commonJS and ES6

Is there any point in me using es6 and mixing it with discordJS? Meaning I will use import and require. Is discordjs ES6 compatible with regards to using import? Currently I have discordjs files saved as .cjs files
11 Replies
d.js toolkit
d.js toolkit4w ago
Amgelo
Amgelo4w ago
why use both? just stick to one, djs supports both
sb
sbOP4w ago
How do I replace this require in ES6?
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
unless importing conditionally is a thing now?
Amgelo
Amgelo4w ago
await import()
d.js docs
d.js docs4w ago
:mdn: import() The import() syntax, commonly called dynamic import, is a function-like expression that allows loading an ECMAScript module asynchronously and dynamically into a potentially non-module environment.
sb
sbOP4w ago
ah how about for commands?
module.exports = {
category: "random",
cooldown: 1,
data: new SlashCommandBuilder()
module.exports = {
category: "random",
cooldown: 1,
data: new SlashCommandBuilder()
export default = { ... }
export default = { ... }
Not sure if ES6 lets me do inline functions like this? has to be a named function, will discordjs be fine with this?
sb
sbOP4w ago
MDN Web Docs
export - JavaScript | MDN
The export declaration is used to export values from a JavaScript module. Exported values can then be imported into other programs with the import declaration or dynamic import. The value of an imported binding is subject to change in the module that exports it — when a module updates the value of a binding that it exports, the update will be ...
Amgelo
Amgelo4w ago
that's not something on djs side though djs doesn't have any command execution logic, that's up to you your actual question should be "will my code be fine with this?" and that depends, I'm not sure how your entire code looks like
sb
sbOP4w ago
yeah good point, I just followed the tutorial on the discordjs website but my guess is when we require the command file, it default exports that function, so I guess I could just define a function and export default newFn
granf
granf4w ago
are you new to js or something? for named export use export {hello1, hello2} for default exports, use export default hello1 and, use it as: import {hello1, hello2} from "./module" or, import defaultExportedFunction from "./module" or, import defaultExportedFunction, {hello1, hello2} from "./module"
d.js toolkit
d.js toolkit3w ago
The issue has been marked as solved by support staff

Did you find this page helpful?