🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
HTML MASTER CLASS /// LEARN TAGS /// BUILD STRUCTURE /// SEMANTIC WEB /// HTML MASTER CLASS /// LEARN TAGS ///

Untitled Lesson

Total XP: 0|💻 backend XP: 0

Skill Matrix

UNLOCK NODES BY LEARNING NEW TAGS.

Select an unlocked node to view details root

Pascual Vila

Pascual Vila

Frontend Instructor // Code Syllabus

Common Pitfalls & Errors

The Error //

Emitting an 'error' event with no registered error listener

// Dangerous: crashes the whole process if this ever emits const emitter = new EventEmitter(); emitter.emit("error", new Error("oops")); // uncaught, process crashes // Correct: always handled emitter.on("error", (err) => logger.error(err)); emitter.emit("error", new Error("oops")); // now safely logged instead

The Solution //

This is the one EventEmitter behavior that differs dangerously from every other event — Node throws the unhandled error and crashes the entire process by default. Always register an explicit error listener on any emitter that might ever emit one, even if it just logs the error.

The Error //

Registering a new listener on a shared, long-lived EventEmitter on every request or connection, without ever removing it

// Wrong: a new listener on every connection, never removed function handleConnection(socket) { eventBus.on("update", () => sendUpdate(socket)); // accumulates forever } // Correct: cleaned up when the connection closes function handleConnection(socket) { const listener = () => sendUpdate(socket); eventBus.on("update", listener); socket.on("close", () => eventBus.off("update", listener)); }

The Solution //

Each registered listener persists indefinitely (holding its entire captured closure in memory) until explicitly removed — in a long-running process, this accumulates listeners without bound, a classic memory leak pattern. Store a reference to the listener and remove it with .off() when the associated request or connection ends.

Continue Learning