node.js - request - How to "emitter.setMaxListeners()"?

node.jsHttprequest

node.js Problem Overview


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?

node.js Solutions


Solution 1 - node.js

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;

Solution 2 - node.js

I use the code to increase the default limit globally:

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

Solution 3 - node.js

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};

Solution 4 - node.js

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) {
}

Solution 5 - node.js

Try to use:

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

Solution 6 - node.js

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

Solution 7 - node.js

It also happened to me

I use this code and it worked

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

Try it out. It may help

Thanks

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questioncamden_kidView Question on Stackoverflow
Solution 1 - node.jsFélix BrunetView Answer on Stackoverflow
Solution 2 - node.jszag2artView Answer on Stackoverflow
Solution 3 - node.jscamden_kidView Answer on Stackoverflow
Solution 4 - node.jsMariya DavydovaView Answer on Stackoverflow
Solution 5 - node.jsMalik Jehangir RiazView Answer on Stackoverflow
Solution 6 - node.jsvijayView Answer on Stackoverflow
Solution 7 - node.jsAKM The GamerView Answer on Stackoverflow