How to fix MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 listeners added in Node.js

Node.jsINTERMEDIATEMEDIUMUpdated Dec 9, 2025· 291 views

This warning appears when more than 10 event listeners are attached to a single EventEmitter instance, indicating a potential memory leak from listeners not being properly cleaned up.

Quick fix

Fixes MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 listeners added

MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
11 connection listeners added to [RedisClient]
  1. Identify which event is leaking listeners
  2. Find where listeners are being added
  3. Use .once() for one-time handlers
  4. Remove listeners when no longer needed
node.js — error
MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
11 connection listeners added to [RedisClient]
MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 listeners added

What this error means

Node.js EventEmitters have a default limit of 10 listeners per event to help detect memory leaks early. When you exceed this limit, Node.js emits a MaxListenersExceededWarning to alert you that listeners are accumulating on an emitter instance. This is a warning, not an error—your code will continue to run. However, it signals that event listeners are being added repeatedly without being removed, which causes memory consumption to grow over time. Each listener holds references to callback functions and their closures, preventing garbage collection. The warning specifically mentions the number of listeners (e.g., "11 listeners") and suggests using `emitter.setMaxListeners()` to increase the limit. However, simply increasing the limit masks the underlying problem rather than fixing it.

Typical symptoms

  • Warning appears in console: "MaxListenersExceededWarning: Possible EventEmitter memory leak detected"
  • Application memory usage gradually increases over time
  • Warning mentions specific number of listeners (typically 11 or more)
  • Same event handler appears to be registered multiple times
  • Warning occurs with HTTP agents, database connections, or socket operations
  • Process output shows the event name causing the warning

Common causes

  1. Adding event listeners inside loops or repeated function calls without cleanup
  2. Using .on() instead of .once() for one-time event handlers
  3. Not removing listeners when components unmount or operations complete
  4. HTTP agents with keepAlive reusing sockets while accumulating error handlers
  5. Creating new listeners on the same emitter in Promise chains without cleanup
  6. Registering listeners in repeated API calls or request handlers
  7. Missing .removeListener() or .removeAllListeners() calls in cleanup code

How to fix "MaxListenersExceededWarning: Possible EventEmitter memory leak detected. 11 listeners added"

1Identify which event is leaking listeners

Examine the warning message to see which event has too many listeners:

bash
MaxListenersExceededWarning: Possible EventEmitter memory leak detected.
11 connection listeners added to [RedisClient]

The warning shows the event name ("connection") and the emitter type ([RedisClient]). This tells you where to focus your investigation.

2Find where listeners are being added

Search your codebase for places where you're adding listeners to this event:

javascript
// Look for patterns like this
emitter.on('connection', handler);
server.on('error', errorHandler);
socket.on('data', dataHandler);

Pay special attention to code inside:
- Request handlers or middleware
- Loops or repeated function calls
- Component lifecycle methods
- Promise chains

3Use .once() for one-time handlers

If the listener should only fire once, use .once() instead of .on():

javascript
// Before (accumulates listeners)
emitter.on('ready', () => {
  console.log('Ready!');
});

// After (auto-removes after first trigger)
emitter.once('ready', () => {
  console.log('Ready!');
});

This is especially important in Promise-based code or initialization logic.

4Remove listeners when no longer needed

Explicitly remove listeners during cleanup:

javascript
function setupHandler() {
  const handler = (data) => {
    console.log('Received:', data);
  };

  emitter.on('data', handler);

  // Later, when done:
  return () => {
    emitter.removeListener('data', handler);
  };
}

// Or remove all listeners for an event:
emitter.removeAllListeners('data');

In React components, remove listeners in cleanup functions:

javascript
useEffect(() => {
  const handler = () => { /* ... */ };
  emitter.on('event', handler);

  return () => {
    emitter.removeListener('event', handler);
  };
}, []);
5Move listener registration outside loops and repeated calls

Avoid registering listeners inside code that runs multiple times:

javascript
// Bad: Adds a new listener on every request
app.get('/api/data', (req, res) => {
  eventBus.on('update', (data) => {
    res.json(data);
  });
});

// Good: Register listener once at startup
eventBus.once('update', (data) => {
  // Handle update
});

app.get('/api/data', (req, res) => {
  // Use the data without re-registering
});
6Verify the fix

Run your application and ensure the warning no longer appears:

bash
node your-app.js

Monitor for the MaxListenersExceededWarning in your logs. If it still appears, continue investigating other locations where listeners might be added.

You can also check current listener counts programmatically:

javascript
console.log('Listener count:', emitter.listenerCount('eventName'));
console.log('All listeners:', emitter.listeners('eventName'));

Advanced notes

When increasing the limit is appropriate:

In rare cases, you legitimately need more than 10 listeners (e.g., a message bus with many subscribers). Only then should you increase the limit:

javascript
// For a specific emitter instance:
emitter.setMaxListeners(20);

// Globally (use with extreme caution):
require('events').EventEmitter.defaultMaxListeners = 15;

// Unlimited (dangerous, only for debugging):
emitter.setMaxListeners(0);

Never use `setMaxListeners(0)` in production—it disables the warning entirely and allows actual memory leaks to go undetected.

HTTP Agent considerations:

When using HTTP/HTTPS agents with keepAlive: true, sockets are reused across requests. If you add error handlers to the socket on each request, they accumulate:

javascript
// Problematic pattern
const agent = new https.Agent({ keepAlive: true });

function makeRequest() {
  const req = https.request({ agent, ... }, (res) => {
    // This adds a new error handler on the same socket each time
    res.socket.on('error', handleError);
  });
}

Solution: Register socket error handlers once, or use .once() for one-time handlers.

Debugging tools:

- Use --trace-warnings flag to see stack traces: node --trace-warnings app.js
- Use process.on('warning', ...) to intercept warnings programmatically
- Tools like clinic, memwatch-next, or Chrome DevTools can help profile memory usage

Frequently asked questions

What does "MaxListenersExceededWarning: Possible EventEmitter memory leak detecte" mean?
js EventEmitters have a default limit of 10 listeners per event to help detect memory leaks early. js emits a MaxListenersExceededWarning to alert you that listeners are accumulating on an emitter instance. This is a warning, not an error—your code will continue to run.
How do I fix "MaxListenersExceededWarning: Possible EventEmitter memory leak detecte"?
This warning appears when more than 10 event listeners are attached to a single EventEmitter instance, indicating a potential memory leak from listeners not being properly cleaned up.
What causes "MaxListenersExceededWarning: Possible EventEmitter memory leak detecte"?
Adding event listeners inside loops or repeated function calls without cleanup Using .on() instead of .once() for one-time event handlers Not removing listeners when components unmount or operations complete
Is the "MaxListenersExceededWarning: Possible EventEmitter memory leak detecte" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading