67

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?

7 Answers 7

115

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;
6
  • 3
    How to identify the cause of the resource leakage? Feb 5, 2018 at 18:29
  • 1
    in short : debugging. Their is many things that can cause resource leakage. and there is some tools that can help detect them. but their is not magic way to find any bug. Feb 7, 2018 at 14:28
  • 5
    You might use require('events').defaultMaxListeners = 15; directly. Mar 2, 2019 at 0:30
  • where to use it in react native Mar 4, 2020 at 13:08
  • I even more strongly advise to use it :D
    – danday74
    Oct 21, 2021 at 16:35
32

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

2
  • 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; Sep 29, 2016 at 15:03
  • this don't work for me. The bellow is working: require('events').defaultMaxListeners = 15; Dec 30, 2020 at 7: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) {
}
1
  • 1
    This gives request.setMaxListeners is not a function error. You have to chain it on request.get(…, function(){…}).setMaxListeners(0) (related ques) Jul 22, 2015 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};
6
  • 'Request' is client request. Am I right ? can you elaborate a bit more ?
    – Riz
    Mar 19, 2012 at 10:15
  • @dev Yes, it's client request. It's the node module 'request'.
    – camden_kid
    Mar 19, 2012 at 10:31
  • added request module. Now can I put that code in callback function of http.createServer ? I tried but warning is still there.
    – Riz
    Mar 19, 2012 at 10:49
  • 2
    process.setMaxListeners(0); doesn't solve my problem. node version: v9.4.0. And also, its not a good practice to edit the code in node_modules rt? Can anyone tells any other solution? Aug 10, 2018 at 6:53
  • This isn't advisable unless you're deliberately creating all those listeners.
    – Slbox
    Sep 3, 2021 at 18:44
0

Try to use:

require('events').EventEmitter.defaultMaxListeners = Infinity; 
-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

-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

1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.