我得到以下警告:

(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);

如何解决这个问题?


当前回答

当我在mac osx上安装aglio时,我也得到了这个警告。

我使用cmd修复它。

sudo npm install -g npm@next

https://github.com/npm/npm/issues/13806

其他回答

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();
    }

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

把它放在你的server.js(或任何包含你的主Node.js应用程序)的第一行:

需要.EventEmitter.prototype(“事件”)。_maxListeners = 0;

错误就消失了:)

这在节点eventEmitter文档中有解释

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

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

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

在我的情况下,这是由于没有关闭到数据库的Sequelize连接,而创建他们内部的异步函数调用setInterval。

将.on()替换为once()。使用once()在同一函数处理事件时删除事件监听器。

如果这不能解决问题,那么在package.json中重新安装restler :“restler git: / / github.com/danwrong/restler.git # 9 d455ff14c57ddbe263dbbcd0289d76413bfe07d”

这与restler 0.10对node的错误行为有关。你可以在这里看到git关闭的问题:https://github.com/danwrong/restler/issues/112 然而,npm还没有更新这个,所以这就是为什么你必须引用git头。