Error when loading events

Hello ! I'm creating a Discord bot with Sapphire and when I create an event, I have this error : Error when loading '/home/desk/bot-sapphire/common/src/ticket/listeners/ready.js': Error: Dynamic require of "/home/desk/bot-sapphire/common/src/ticket/listeners/ready.js" is not supported I'm using ESM! Thanks
Solution:
Ok I undertstand ... I don't use .mjs extension on my file ... But I don't understand here : https://github.com/sapphiredev/pieces/blob/main/src/lib/strategies/LoaderStrategy.ts#L73 How does it work ? If I use ESM, we should use import() instead of require(), no ?...
GitHub
pieces/src/lib/strategies/LoaderStrategy.ts at main · sapphiredev/p...
Piece loader for @sapphire/framework. Contribute to sapphiredev/pieces development by creating an account on GitHub.
Jump to solution
6 Replies
Favna
Favna12mo ago
Show code please.
Mathias
MathiasOP12mo ago
My new SapphireClient in a different npm workspace:
import { SapphireClient } from '@sapphire/framework';
import { getRootData } from '@sapphire/pieces';
import { join } from 'node:path';

export class MCSapphireClient extends SapphireClient {
rootData = getRootData();

constructor(options) {
super(options);

options.modules?.forEach((module) =>
this.stores.registerPath(join(this.rootData.root, module))
);
}
}
import { SapphireClient } from '@sapphire/framework';
import { getRootData } from '@sapphire/pieces';
import { join } from 'node:path';

export class MCSapphireClient extends SapphireClient {
rootData = getRootData();

constructor(options) {
super(options);

options.modules?.forEach((module) =>
this.stores.registerPath(join(this.rootData.root, module))
);
}
}
My index.js in another npm workspace (my bot) :
import { GatewayIntentBits } from "discord.js";
import { MCSapphireClient } from "services";
import config from "../../config.json" assert { type: "json" };

const client = new MCSapphireClient({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
modules: [
"ticket"
]
});

client.config = config;

client.login(client.config.TOKEN);
import { GatewayIntentBits } from "discord.js";
import { MCSapphireClient } from "services";
import config from "../../config.json" assert { type: "json" };

const client = new MCSapphireClient({
intents: [GatewayIntentBits.Guilds, GatewayIntentBits.GuildMessages],
modules: [
"ticket"
]
});

client.config = config;

client.login(client.config.TOKEN);
My ready event:
import { Listener } from '@sapphire/framework';

export class ReadyListener extends Listener {
run(client) {
this.container.logger.info(`Hello !`);
}
}
import { Listener } from '@sapphire/framework';

export class ReadyListener extends Listener {
run(client) {
this.container.logger.info(`Hello !`);
}
}
Full logs:
Error when loading '/home/desk/bot-sapphire/common/src/ticket/listeners/ready.js': Error: Dynamic require of "/home/desk/bot-sapphire/common/src/ticket/listeners/ready.js" is not supported
at file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/chunk-LIQV5WSN.mjs:9:9
at _ListenerLoaderStrategy.preload (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/strategies/LoaderStrategy.mjs:43:17)
at _ListenerLoaderStrategy.load (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/strategies/LoaderStrategy.mjs:50:31)
at load.next (<anonymous>)
at _ListenerStore.loadPath (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/structures/Store.mjs:273:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async _ListenerStore.loadAll (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/structures/Store.mjs:185:24)
at async Promise.all (index 3)
at async MCSapphireClient.login (file:///home/desk/bot-sapphire/node_modules/@sapphire/framework/dist/esm/lib/SapphireClient.mjs:78:5)
Error when loading '/home/desk/bot-sapphire/common/src/ticket/listeners/ready.js': Error: Dynamic require of "/home/desk/bot-sapphire/common/src/ticket/listeners/ready.js" is not supported
at file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/chunk-LIQV5WSN.mjs:9:9
at _ListenerLoaderStrategy.preload (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/strategies/LoaderStrategy.mjs:43:17)
at _ListenerLoaderStrategy.load (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/strategies/LoaderStrategy.mjs:50:31)
at load.next (<anonymous>)
at _ListenerStore.loadPath (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/structures/Store.mjs:273:26)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async _ListenerStore.loadAll (file:///home/desk/bot-sapphire/node_modules/@sapphire/pieces/dist/esm/lib/structures/Store.mjs:185:24)
at async Promise.all (index 3)
at async MCSapphireClient.login (file:///home/desk/bot-sapphire/node_modules/@sapphire/framework/dist/esm/lib/SapphireClient.mjs:78:5)
I think it's because I'm using npm package My MCSapphireClient is in the package3, my bot in the package1 and the dependency in node_modules, on the root of the project
Solution
Mathias
Mathias12mo ago
Ok I undertstand ... I don't use .mjs extension on my file ... But I don't understand here : https://github.com/sapphiredev/pieces/blob/main/src/lib/strategies/LoaderStrategy.ts#L73 How does it work ? If I use ESM, we should use import() instead of require(), no ?
GitHub
pieces/src/lib/strategies/LoaderStrategy.ts at main · sapphiredev/p...
Piece loader for @sapphire/framework. Contribute to sapphiredev/pieces development by creating an account on GitHub.
Mathias
MathiasOP12mo ago
Why does it work ?
Favna
Favna12mo ago
Can you please put all your code on GitHub? Your setup is making things very complex with the whole multiple packages setup. I hope you have a good reason for that because otherwise you're overcomplicating for no good reason.
Mathias
MathiasOP12mo ago
The problem's already been solved! It's because I was using the .js extension and not .mjs. And I didn't know you could require a .js file with ESM. It's all a bit new, so I'm trying to learn as I go! It's because I'd like to make 3 different bots with common components So I have 4 npm workspaces: 3 for a bot and 1 for the common components
Want results from more Discord servers?
Add your server