Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 505
camden_kid Avatar asked Nov 29 '11 15:11

camden_kid


People also ask

How do I use event emitters in Node JS?

Many objects in a Node emit events, for example, a net. Server emits an event each time a peer connects to it, an fs. readStream emits an event when the file is opened. All objects which emit events are the instances of events.

What is emitter in Node JS?

The EventEmitter is a module that facilitates communication/interaction between objects in Node. EventEmitter is at the core of Node asynchronous event-driven architecture. Many of Node's built-in modules inherit from EventEmitter including prominent frameworks like Express. js.

How do I require a request in Node JS?

After installing request module you can check your request version in command prompt using the command. 3. After that, you can create a folder and add a file for example index. js, To run this file you need to run the following command.


1 Answers

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; 
like image 56
Félix Brunet Avatar answered Sep 27 '22 19:09

Félix Brunet