What is the events module and EventEmitter in Node.js?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Node.js topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
The events module provides the EventEmitter class, which is the foundation of Node.js's event-driven architecture. EventEmitter allows objects to emit named events and register listener functions that respond to those events. Key methods: emitter.on("event", listener) — add a listener (persists); emitter.once("event", listener) — add a one-time listener (auto-removed after first call); emitter.emit("event", ...args) — trigger all listeners for the event synchronously; emitter.off("event", listener) / removeListener() — remove a specific listener; emitter.removeAllListeners("event") — remove all listeners. Many core Node.js classes extend EventEmitter: HTTP servers ("request"), streams ("data", "end"), and child processes ("exit"). Custom EventEmitters are useful for decoupling components in your application through a publish-subscribe pattern.
Pro Tip
This topic has Node.js-specific nuances that differ from general programming. Highlighting those nuances in your answer shows expertise rather than generic knowledge.
Previous
What is process in Node.js?
Next
What is the difference between synchronous and asynchronous code in Node.js?