codegurukool.com · Free coding tutorials & online compiler

Home › Node.js Tutorial › Node.js events

Node.js events

React to things that happen.

The EventEmitter class lets objects announce events. Listen with on and trigger with emit.

Example code

const EventEmitter = require("events");
const e = new EventEmitter();
e.on("greet", function (n) { console.log("Hi " + n); });
e.emit("greet", "Asha");

Run this code in the online editor

← Node.js file systemNode.js async and await →