Search code examples
node.jsnode-rest-client

EventEmitter memory leak detected with node-rest-client


During the execution of my mocha testsuit, the following warning is reported by Node.js:

(node) warning: possible EventEmitter memory leak detected. 11 error listeners a
dded. Use emitter.setMaxListeners() to increase limit.
Trace
    at Object.addListener (events.js:179:15)
    at new exports.Client (C:\xyz\node_modules\node-rest-client\lib\node-rest-
client.js:320:17)
    at Context.<anonymous> (C:\xyz\test\backend\rest\resources.js:40:10)
...

I strongly suspect this has to do with node-rest-client module, that I use. The last showed line, indicated in the warning, is actually:

rest = new Client();

If I execute only a single testcase which throws this warning, the warning does not shows up. It happens only when I execute the whole testsuite, with around 15 new Client() lines.

I have not found a way to somehow close the rest client, so I now tried simply with:

delete rest

It did not help to remove the warning.

Any clues?


Solution

  • This isn't necessarily a problem, more a warning of the system to make sure that you know what you're doing.

    Because of the internals of node-rest-client you can't change it specifically for that module (see also this issue), but to get rid of the warning during your tests, put this somewhere at the top of your code:

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

    More information here.