我得到以下警告:

(node) warning: possible EventEmitter memory leak detected. 11 listeners added. Use emitter.setMaxListeners() to increase limit.
Trace: 
    at EventEmitter.<anonymous> (events.js:139:15)
    at EventEmitter.<anonymous> (node.js:385:29)
    at Server.<anonymous> (server.js:20:17)
    at Server.emit (events.js:70:17)
    at HTTPParser.onIncoming (http.js:1514:12)
    at HTTPParser.onHeadersComplete (http.js:102:31)
    at Socket.ondata (http.js:1410:22)
    at TCP.onread (net.js:354:27)

我在server.js中写了这样的代码:

http.createServer(
    function (req, res) { ... }).listen(3013);

如何解决这个问题?


当前回答

这在节点eventEmitter文档中有解释

这是什么版本的Node ?你还有其他代码吗?这不是正常的行为。

简而言之,它:process.setMaxListeners(0);

另见:node.js - request -如何“emitter.setMaxListeners()”?

其他回答

我想在这里指出,这个警告是有原因的,正确的解决方法很可能不是增加限制,而是弄清楚为什么要向同一个事件添加如此多的侦听器。只有当您知道为什么要添加这么多侦听器并且确信这是您真正想要的时候,才增加限制。

我找到了这个页面,因为我得到了这个警告,在我的情况下,我正在使用的一些代码中有一个错误,将全局对象转换为EventEmitter!我当然不建议在全球范围内增加限制,因为你不想让这些事情被忽视。

You said you are using process.on('uncaughtException', callback); Where are you executing this statement? Is it within the callback passed to http.createServer?If yes, different copy of the same callback will get attached to the uncaughtException event upon each new request, because the function (req, res) { ... } gets executed everytime a new request comes in and so will the statement process.on('uncaughtException', callback);Note that the process object is global to all your requests and adding listeners to its event everytime a new request comes in will not make any sense. You might not want such kind of behaviour. In case you want to attach a new listener for each new request, you should remove all previous listeners attached to the event as they no longer would be required using: process.removeAllListeners('uncaughtException');

添加EventEmitter.defaultMaxListeners = <MaxNumberOfClients>到node_modules\loop -datasource-juggler\lib\datasource.js修复了可能的问题:)

I prefer to hunt down and fix problems instead of suppressing logs whenever possible. After a couple days of observing this issue in my app, I realized I was setting listeners on the req.socket in an Express middleware to catch socket io errors that kept popping up. At some point, I learned that that was not necessary, but I kept the listeners around anyway. I just removed them and the error you are experiencing went away. I verified it was the cause by running requests to my server with and without the following middleware:

socketEventsHandler(req, res, next) {
        req.socket.on("error", function(err) {
            console.error('------REQ ERROR')
            console.error(err.stack)
        });
        res.socket.on("error", function(err) {
            console.error('------RES ERROR')
            console.error(err.stack)
        });
        next();
    }

删除该中间件将停止您所看到的警告。我会查看您的代码,并尝试找到您可能设置了不需要的侦听器的任何地方。

在创建新的侦听器之前,您需要使用以下命令清除所有侦听器:

客户端/服务器

socket.removeAllListeners(); 

假设socket是您的客户端套接字/或创建的服务器套接字。

你也可以订阅特定的事件监听器,比如像这样删除连接监听器:

this.socket.removeAllListeners("connect");