66

When I do a GET on a certain URI using the node.js 'request' module;

var options = {uri:"aURI", headers:headerData};
request.get(options, function (error, response, body) {
}

The error message is:

[Error: Exceeded maxRedirects. Probably stuck in a redirect loop.]

and there is also the following message:

"(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit."

How do I setMaxListeners?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
camden_kid
  • 12,591
  • 11
  • 52
  • 88

7 Answers7

113

I strongly advice NOT to use the code:

process.setMaxListeners(0);

The warning is not there without reason. Most of the time, it is because there is an error hidden in your code. Removing the limit removes the warning, but not its cause, and prevents you from being warned of a source of resource leakage.

If you hit the limit for a legitimate reason, put a reasonable value in the function (the default is 10).

Also, to change the default, it is not necessary to mess with the EventEmitter prototype. you can set the value of defaultMaxListeners attribute like so:

require('events').EventEmitter.defaultMaxListeners = 15;
bschlueter
  • 3,817
  • 1
  • 30
  • 48
Félix Brunet
  • 1,993
  • 1
  • 18
  • 22
32

I use the code to increase the default limit globally: require('events').EventEmitter.prototype._maxListeners = 100;

zag2art
  • 4,869
  • 1
  • 29
  • 39
  • 1
    Had this problem in jenkins running a casperjs script, added this line at top of script and it works!. Thanks var y = require('events').EventEmitter.prototype._maxListeners = 100; – user2908957 Sep 29 '16 at 15:03
  • this don't work for me. The bellow is working: require('events').defaultMaxListeners = 15; – Debashis Chowdhury Dec 30 '20 at 07:26
7

Although adding something to nodejs module is possible, it seems to be not the best way (if you try to run your code on other computer, the program will crash with the same error, obviously).

I would rather set max listeners number in your own code:

var options = {uri:headingUri, headers:headerData, maxRedirects:100};
request.setMaxListeners(0);
request.get(options, function (error, response, body) {
}
Mariya Davydova
  • 1,353
  • 9
  • 14
  • 1
    This gives `request.setMaxListeners is not a function` error. You have to chain it on `request.get(…, function(){…}).setMaxListeners(0)` ([related ques](http://stackoverflow.com/questions/16093457/has-no-method-setmaxlisteners-error)) – laggingreflex Jul 22 '15 at 18:24
6

This is how I solved the problem:

In main.js of the 'request' module I added one line:

Request.prototype.request = function () {
  var self = this
  self.setMaxListeners(0); // Added line

This defines unlimited listeners http://nodejs.org/docs/v0.4.7/api/events.html#emitter.setMaxListeners

In my code I set the 'maxRedirects' value explicitly:

var options = {uri:headingUri, headers:headerData, maxRedirects:100};
camden_kid
  • 12,591
  • 11
  • 52
  • 88
0

Try to use:

require('events').EventEmitter.defaultMaxListeners = Infinity; 
turivishal
  • 34,368
  • 7
  • 36
  • 59
-3

this is Extension to @Félix Brunet answer

Reason - there is code hidden in your app

How to find -

  • Strip/comment code and execute until you reach error
  • check log file

Eg - In my case i created 30 instances of winston log Unknowingly and it started giving error

Note : if u supress this error , it will come again afetr 3..4 days

vijay
  • 10,276
  • 11
  • 64
  • 79
-3

It also happened to me

I use this code and it worked

require('events').EventEmitter.defaultMaxListeners = infinity;

Try it out. It may help

Thanks