possible EventEmitter memory leak detected – Dev

The best answers to the question “possible EventEmitter memory leak detected” in the category Dev.


I am getting following warning:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:385:29)
    at Server.<anonymous> (server.js:20:17)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1410:22)
    at TCP.onread (net.js:354:27)

I wrote code like this in server.js:

    function (req, res) { ... }).listen(3013);

How to fix this ?


This is explained in the node eventEmitter documentation

What version of Node is this? What other code do you have? That isn’t normal behavior.

In short, its: process.setMaxListeners(0);

Also see: node.js – request – How to “emitter.setMaxListeners()”?


I’d like to point out here that that warning is there for a reason and there’s a good chance the right fix is not increasing the limit but figuring out why you’re adding so many listeners to the same event. Only increase the limit if you know why so many listeners are being added and are confident it’s what you really want.

I found this page because I got this warning and in my case there was a bug in some code I was using that was turning the global object into an EventEmitter! I’d certainly advise against increasing the limit globally because you don’t want these things to go unnoticed.


By default, a maximum of 10 listeners can be registered for any single event.

If it’s your code, you can specify maxListeners via:

const emitter = new EventEmitter()
// or 0 to turn off the limit

But if it’s not your code you can use the trick to increase the default limit globally:

require('events').EventEmitter.prototype._maxListeners = 100;

Of course you can turn off the limits but be careful:

// turn off limits by default (BE CAREFUL)
require('events').EventEmitter.prototype._maxListeners = 0;

BTW. The code should be at the very beginning of the app.

ADD: Since node 0.11 this code also works to change the default limit:

require('events').EventEmitter.defaultMaxListeners = 0


The accepted answer provides the semantics on how to increase the limit, but as @voltrevo pointed out that warning is there for a reason and your code probably has a bug.

Consider the following buggy code:

//Assume Logger is a module that emits errors
var Logger = require('./Logger.js');

for (var i = 0; i < 11; i++) {
    //BUG: This will cause the warning
    //As the event listener is added in a loop
    Logger.on('error', function (err) {
        console.log('error writing log: ' + err)


Now observe the correct way of adding the listener:

//Good: event listener is not in a loop
Logger.on('error', function (err) {
    console.log('error writing log: ' + err)

for (var i = 0; i < 11; i++) {

Search for similar issues in your code before changing the maxListeners (which is explained in other answers)