What do you understand by an EventEmitter in Node.js?

In Node.js, an EventEmitter is a class that includes all the objects capable of emitting events. This can be achieved by attaching named events that are emitted by the object using an eventEmitter.on() function. Thus whenever this object throws an event, the attached functions are invoked synchronously.


  1. const EventEmitter = require(‘events’);  
  2. class MyEmitter extends EventEmitter {}  
  3. const myEmitter = new MyEmitter();  
  4. myEmitter.on(‘event’, () => {  
  5.  console.log(‘an event occurred!’);  
  6. });  
  7. myEmitter.emit(‘event’);