Home >> Tutorials >> Node Package Manager (NPM)
Previous Page

Events and EventEmitter in Node.js:

Node.js a built-in module called "events".

  • In Node.js, EventEmitter is used to "emit" an errors.
  • In Node.js please ensure that if any event emitters emit an error that event should be listened properly, otherwise your app will crash.
const EventEmitter = require('events');
 
//Creating object of EventEmitter class
const emitter = new EventEmitter();
 
// Listening the event
emitter.on('logs', function() {
 console.log('Hello, welcome to EventEmitter in Node.js!')
})
 
// Creating an event
emitter.emit('logs');

logs is the name of the event.

Note: Always register event first, then emit or raise the event.

Previous Page