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


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?

2/22/2016 9:54:01 PM

I strongly advice NOT to use the code:


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;
7/19/2018 6:03:46 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow