Common Built-in utilities

Last Updated : 2026


Node.js comes with a number of built-in utilities that are used to perform common tasks. These utilities are built-in modules that are automatically loaded when you import them.

Syntax for importing a built-in module:

  • CommonJS:
const moduleName = require("node:moduleName"); // Modern syntax(Recommended)
//  or
const moduleName = require("moduleName");
  • ESM:
import moduleName from "node:moduleName"; //(Modern syntax(Recommended))
// or
import moduleName from "moduleName";

Note: node: prefix is used to indicate that the module is a built-in module.

Some of the most commonly used built-in modules are:

  • events: For working with events.
  • process: For working with the current Node.js process.
  • path: For working with file and directory paths.
  • os: For accessing operating system information.
  • fs: For interacting with the file system.
  • Crypto: For working with cryptographic operations.
  • url: For working with URLs.
  • http: For creating HTTP servers and clients.

So we are going to Cover them one by One ,So let's get started with the events module.

events

Node.js is event-driven. In fact, core modules like http servers and file streams are built on top of the EventEmitter class!

It uses a simple Publish-Subscribe (Pub/Sub) pattern:

  • Listener (.on()): Listens for a specific event name and runs a callback when triggered.
  • Emitter (.emit()): Triggers the event and passes data to all listening callbacks.

You can also use once method to listen for an event only once.

Import the events module using CommonJS:

const events = require("node:events");

Import the events module using ESM:

import { EventEmitter } from "node:events";

Some of the most common events in Node.js are:

  • on
// Create a custom emitter instance
const userEmitter = new EventEmitter();

// 1. Register a Listener (Subscriber)
userEmitter.on("userRegistered", (data) => {
  console.log(`[Email Service]: Sending welcome email to ${data.email}...`);
});

userEmitter.on("userRegistered", (data) => {
  console.log(`[SMS Service]: Sending welcome from ${data.website}...`);
});
  • emit
// 2. Emit an event (Publisher)
userEmitter.emit("userRegistered", {
  website: "tukkalearn.com",
  email: "tukkalearn@gmail.com",
});

Note : Notice how a single emit() action triggered multiple independent handlers! This decouples your code cleanly.


Liked This Tutorial

Scan QR Code To Leave A Review Or

Click Here To Leave A Review
Scan QR