Search code examples
javascriptnode.jsdiscord.jseventemitternodemon

Does the setMaxListeners warning affect my discord.js code?


As I was restarting my code using nodemon, I got the following warning:

(node:11576) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 message listeners added. Use emitter.setMaxListeners() to increase limit.

Code: to Ubuntu Pastebin Link

I believe this is caused by the fact that I have >10 client.on(); scripts. Here's my logic for it: if client is what runs the EventEmitter, and each client.on is a Listener, then there would be over 10 listeners.

I've tried just putting emitter.setMaxListeners(Infinity), but I get the following error (note that line numbers may not be accurate):

/home/discord/Desktop/channel.js:9
emitter.setMaxListeners
^

ReferenceError: emitter is not defined 
    at Object.<anonymous> (/home/discord/Desktop/channel.js:9:1)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at Startup (internal/bootstrap/node.js:283:19)
    at bootsrapNodeJSCore (internal/bootstrap/node.js:743:3)
[nodemon] app crashed - waiting for file changes before starting...

What should I add to fix this? I'm worried some of my client.on subscripts may not work run.


Solution

  • In your case emitter is your Discord.js client.
    So you can use this to allow Infinity listeners:

    client.setMaxListeners(0);
    

    But this is not advisable because there's probably a memory leak somewhere in your code.