19

I have an error and I don't know how to solve it. It happens only some times.

Error message:

(node:9140) MaxListenersExceededWarning: Possible EventEmitter memory leak detec
ted. 11 error listeners added. Use emitter.setMaxListeners() to increase limit
Stepan Rafael
  • 365
  • 2
  • 4
  • 13
  • This does not provide enough information. Add your JS code and package.json. As the message explains there is a way to avoid this warning by increasing the limit, but first you should scan your code to see if any unnecessary listeners are being attached to events. – Vasan Apr 20 '18 at 16:32
  • My JS code is too long to be posted here, same for package.json – Stepan Rafael Apr 20 '18 at 16:34
  • 1
    Well, without that it isn't possible to zero-in on the specific issue you're facing. Only general guidelines can be offered. Try the recommendations [here](https://stackoverflow.com/questions/9768444/possible-eventemitter-memory-leak-detected) – Vasan Apr 20 '18 at 16:36

1 Answers1

16

This error often occurs when you are using EventEmitters directly or indirectly in your code and you are creating too many in too short a period for them to be resolved -- Node detects this as a memory leak and throws an error once the Max Listener count has been exceeded.

For example, it's often common in unit tests to setup and tear-down pre-conditions before and after each test. Test runners, like Mocha, will often run tests in parallel. If you have dozens of tests then you can quickly run the Event Listener count over the maximum if your setup performs operations that emit events (e.g. Connecting to a Database).

Without your specific code, it would be difficult to pinpoint the cause. I recommend you review your code for any Event Emitters that you may have used, either directly or in modules that you are including, and look for any instances where you may be inadvertently creating too many of them in parallel (e.g. through Promises or async). The key is to look for places in your code where you have a lot of "parallel execution" such as loops with Promises. For context, parallel execution is in quotes here because of the pseudo-parallel nature of the NodeJs Event Loop

By default Node usually only allows a maximum of 10 listeners. You can override the number of emitters Node will allow using:

setMaxListeners(n);

However, you should be aware that this is just a warning and it is intended to assist the developer by warning them when they have code that may be causing a memory leak.

JayReardon
  • 809
  • 1
  • 10
  • 22
  • Jay, thanks a lot for your reply, you are great. To be honest, the problem is still there. I had the same warning some time ago that doesn't stop any block code and he dispear with no reason. I don't know what can I do, maybe will disappear again. In your answer, you have right, I use a lot of Promises and async querys to DB. Without these functions my code will not work. However, I will try to reduce this and make some advanced querys to minimize the number of total Promises. – Stepan Rafael May 01 '18 at 00:03
  • 1
    Hey Stepan, the reason why the issues appear to come and go without seeming cause is due to the Promises and asynchronous code executing towards a race condition. If you have several promises with event emitters that take longer to complete, then eventually this will happen. If you can provide the npm module you are using for database connectivity, we should be able to pinpoint the cause of the emitters. Most DB connectivity modules and ORMs emit events for things like connection open, close, and errors. For example the redis module does this -- https://www.npmjs.com/package/redis#api – JayReardon May 04 '18 at 17:08
  • 1
    In Node its process.setMaxListeners(n) – danday74 Oct 21 '21 at 16:35
  • so setMaxListeners(n) just changes when the warning is showed? It is not an actual max? – user1689987 Jul 25 '23 at 19:09