Home > Software design >  What is causing the Node.js "MaxListenersExceededWarning" warning?
What is causing the Node.js "MaxListenersExceededWarning" warning?

Time:07-17

I have this simple Express.js back-end server:

const app = require("express")();
const dotenv = require("dotenv");
const bodyParser = require("body-parser");
const cookieParser = require("cookie-parser");

const swaggerUi = require("swagger-ui-express");
const swaggerJsDocs = require("swagger-jsdoc");

const swaggerOptions = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: 'API',
      version: '0.0.1',
      description: 'Api docs .'
    },
  },
  apis: ["./src/swagger/swagger.yaml"]
};
const swaggerDocs = swaggerJsDocs(swaggerOptions);

dotenv.config();

const server = require("http").createServer(app);

app.use(cookieParser());
app.use(bodyParser.json({limit: '10MB'}));
app.use(bodyParser.urlencoded({extended: true}));
app.use("/api-docs", swaggerUi.serve, swaggerUi.setup(swaggerDocs));
app.use("/", require('./src/routes'));

const port = process.env.PORT || 3001;
server.listen(port, () => console.log("API listening on port "   port   "!"));

But I keep getting this warning and I actually have no idea about what is the reason for it. I don't have web sockets or something, but still:

(node:7847) MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 
11 uncaughtException listeners added to [process]. 
Use emitter.setMaxListeners() to increase limit

CodePudding user response:

That specific error means that your code (or the modules you are using) have added 11 handlers for the process.on('uncaughtException', ...) event.

This is typically an indication that some code is repeatedly adding the same event handler over and over again (in some other event handler such as inside an Express route) where they accumulate more and more forever. This could be in either your own code or in some module that your code imports.

Event handlers like this should be added only once for a given section of code or should be added, then removed when no longer needed.

Based on the small amount of code you show here, I'd guess that the first place to look would be in ./src/routes for any code that adds an uncaughtException listener in one of your route handlers.

CodePudding user response:

I have found the reason of this warning.

In this project I use winston and winston-daily-rotate-file, for every controller and service (to write down logs) I create this logger instance, and basically this creates those handlers:

const loggerInstance = createLogger({
  level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
  format: combine(
    colorize(),
    json(),
    label({ label: data.label ? data.label : 'NONE' }),
    timestamp(),
    myFormat,
  ),
  transports: transportsConfig,
});

if (process.env.NODE_ENV !== 'production') {
  loggerInstance.add(new transports.Console({
    handleExceptions: true,
  }));
}

if (process.env.DISABLE_LOGGER === 'yes') {
  loggerInstance.silent = true;
}

return loggerInstance;
  • Related