Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Has no method 'setMaxListeners' error

Has no method 'setMaxListeners' error

Problem

According to this question here I need to call request.setMaxListeners(0) to solve the problem described.

When I try to do:

var request = require('request');
request.setMaxListeners(0);

I get the following error message:

request.setMaxListeners(0);
        ^ TypeError: Object function request(uri, options, callback) {   if (typeof uri === 'undefined') throw new Error('undefined is not a valid uri or options object.')   if ((typeof options === 'function') && !callback) callback = options   if (options && typeof options === 'object') {
    options.uri = uri   } else if (typeof uri === 'string') {
    options = {uri:uri}   } else {
    options = uri   }

  options = copy(options)

  if (callback) options.callback = callback   var r = new Request(options)   return r } has no method 'setMaxListeners'
    at Object. (/home/vagrant/twitter/test.js:3:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3

How do I correctly adjust the default value for setMaxListeners when using the request-module?

node --version: 0.10.4
request module version: 2.16.6

Problem courtesy of: yas4891

Solution

EDIT

After a bit disscussion i think you are trying to do something like this, after "setMaxListeners"

var request = require('request');

// this will fail
request.setMaxListeners(0);

request('http://www.google.com', function (error, response, body) {
  // do something ...
})

Step by step

var request = require('request');

The request module is required. This module exports the Function request.

This function does not inherit from the EventEmitter - an internal node "class" - so it does not have the "setMaxListeners method".

With this in mind, the following line will fail

request.setMaxListeners(0);

You need a Request object, that inherits from the EventEmitter and has the desired method.

To get one, simply call the request function. On the value returned by this function, you can call "setMaxListeners".

var request_object = request('http://www.google.com')
request_object.setMaxListeners(0)

Better if we chain the calls

request('http://www.google.com').setMaxListeners(0)

Warning

I do not recomend at all removing the max listeners limit. Some kind of infinite loop may be the problem - maybe a listener being bound on each "request" event - to throw a "too much listeners bound" error. This error is an effective method to detect memory leaks, so i recommend raising a bit the "maxListeners" limit rather than setting it to "unlimited".

To do this, pass a int != 0. As a note, the default value is 10.

request(
  //
  // request stuff goes here
  //
).setMaxListeners(20)

The Answer

var request = require('request');

request('http://www.google.com', function (error, response, body) {
  //
  // do something ...
  //
}).setMaxListeners(20)
Solution courtesy of: laconbass

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

Has no method 'setMaxListeners' error

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×