MaxListenersExceededWarning: Possible EventEmitter memory leak..

I recently switched my project from CJS to ESM, and while nothing in my code actually changed other than my import/export syntax, I am now getting the following alert which never happened in the years of running the bot before the changes
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 undefined listeners added to [Client]. Use emitter.setMaxListeners() to increase limit
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 undefined listeners added to [Client]. Use emitter.setMaxListeners() to increase limit
I have added setMaxListeners(20) to suppress the alert in the mean time, which it has, but I'm curious to know where this has come from and if I need to look into it more? I do have about 16 client event listeners, and I only use the intents/partials that I require
7 Replies
d.js toolkit
d.js toolkit10mo 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
duck
duck10mo ago
11 undefined listeners added to [Client]
I imagine you're using the Events enum and didn't properly migrate how it was imported to esm
zevnda
zevndaOP10mo ago
So when I log client._events I get the following output. These appear to be all the events that I am listening to, but still getting that same warning
{
shardDisconnect: [Function (anonymous)],
ready: [AsyncFunction (anonymous)],
channelDelete: [Function (anonymous)],
emojiDelete: [Function (anonymous)],
roleDelete: [Function (anonymous)],
stickerDelete: [Function (anonymous)],
threadCreate: [Function (anonymous)],
threadDelete: [Function (anonymous)],
guildMemberRemove: [Function (anonymous)],
guildMemberUpdate: [Function (anonymous)],
messageUpdate: [Function (anonymous)],
inviteCreate: [Function (anonymous)],
inviteDelete: [Function (anonymous)],
messageDelete: [Function (anonymous)],
interactionCreate: [Function (anonymous)],
guildMemberAdd: [Function (anonymous)],
messageCreate: [Function (anonymous)]
}
{
shardDisconnect: [Function (anonymous)],
ready: [AsyncFunction (anonymous)],
channelDelete: [Function (anonymous)],
emojiDelete: [Function (anonymous)],
roleDelete: [Function (anonymous)],
stickerDelete: [Function (anonymous)],
threadCreate: [Function (anonymous)],
threadDelete: [Function (anonymous)],
guildMemberRemove: [Function (anonymous)],
guildMemberUpdate: [Function (anonymous)],
messageUpdate: [Function (anonymous)],
inviteCreate: [Function (anonymous)],
inviteDelete: [Function (anonymous)],
messageDelete: [Function (anonymous)],
interactionCreate: [Function (anonymous)],
guildMemberAdd: [Function (anonymous)],
messageCreate: [Function (anonymous)]
}
duck
duck10mo ago
in case it wasn't clear, the relevant information was that it says 11 undefined listeners which is to say, it believes the event name is undefined as a rule of thumb I tend to not rely on private properties, but putting that aside, I do see that this output does not have undefined listed, so it sounds like the undefined listeners are being added after this but regardless it'll be hard to help further without seeing any code
zevnda
zevndaOP10mo ago
Roger that, I did double check if the event name values were correct, and they were. Here is my event handler
import { promisify } from 'util';
import glob from 'glob';
const PG = promisify(glob);

export default async (client, Discord) => {
(await PG(`${process.cwd()}/events/*/*.js`)).map(async (file) => {
const { default: event } = await import('file://' + file);
console.log(event.name)
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client, Discord));
} else {
client.on(event.name, (...args) => event.execute(...args, Discord));
}
});
};
import { promisify } from 'util';
import glob from 'glob';
const PG = promisify(glob);

export default async (client, Discord) => {
(await PG(`${process.cwd()}/events/*/*.js`)).map(async (file) => {
const { default: event } = await import('file://' + file);
console.log(event.name)
if (event.once) {
client.once(event.name, (...args) => event.execute(...args, client, Discord));
} else {
client.on(event.name, (...args) => event.execute(...args, Discord));
}
});
};
Here is a snippet of how I'm applying those name values
export default {
name: 'ready',
once: true,
async execute(message, client, Discord) {
console.log(client._events)
//...
export default {
name: 'ready',
once: true,
async execute(message, client, Discord) {
console.log(client._events)
//...
As far as I can tell, everything is imported and exported as expected, but I very well might be missing something Excuse the ping, force of habit hitting reply Also those are the only listeners I'm using, so I'd be confused how any more are being added after the fact If you need anything else, let me know
duck
duck10mo ago
personally I don't mind being pinged if I'm already relevant to a conversation there doesn't appear to be anything overtly wrong with just this code you've shown I suppose you could use the --trace-warnings flag to confirm where the given undefined listener is being added (as your warning probably mentioned)
zevnda
zevndaOP10mo ago
For sure, I'll give it a try and see what I can find. Might also be worth mentioning that the warning will randomly show up. I'd expect it to show up around the time I run the bot, seeing as that is when I'm importing those events, but it's usually within 10-30 minutes after. I have doubled checked to see if I might just randomly be registering a listener somewhere throughout my code, but I can't find anything Appreciate the help, will update if I find anything Well I've been running it with the trace warnings flag for about an hour now with no max listener warning, so I'm going to assume something I've done fixed it 🤷🏽‍♂️
Want results from more Discord servers?
Add your server