TExcelTAD
Home › Node.js › Node.js Built-in Modules

Node.js Built-in Modules

Node ships with a standard library of built-in modules (also called core modules). They need no installation: just import them. Since Node 16 you can, and should, write them with the node: prefix, which makes it obvious that the module comes from Node itself and not from npm.

js
const fs = require("node:fs");          // CommonJS
import path from "node:path";           // ES module
import { readFile } from "node:fs/promises";

The most useful built-ins

Module What it is for Lesson
node:fs, node:fs/promises read and write files and folders File System
node:path join and split file paths portably Path
node:os information about the operating system OS
node:events the EventEmitter class Events
node:http, node:https HTTP servers and clients HTTP
node:url parse URLs, convert file paths to URLs URL
node:stream process data piece by piece Streams
node:buffer binary data (Buffer is also global) Buffers
node:crypto hashes, encryption, random IDs Crypto
node:util promisify, inspect, parseArgs, styleText Util
node:child_process run other programs Child Processes
node:worker_threads run JavaScript on other threads Worker Threads
node:zlib gzip / brotli compression Zlib
node:readline read input line by line Readline
node:test, node:assert the built-in test runner and assertions Testing
node:sqlite an embedded SQLite database (22.5+) Databases

Listing every built-in module

The node:module module exposes the full list. There are many internal helpers (names starting with _); here we filter them out:

node · try it yourself
const { builtinModules } = require("node:module");

const publicModules = builtinModules.filter((m) => !m.startsWith("_"));
const wanted = ["fs", "path", "http", "events", "crypto", "stream", "util", "os"];

console.log("All of these are built in:", wanted.every((m) => publicModules.includes(m)));
console.log("Has fs/promises:", publicModules.includes("fs/promises"));
console.log("Is 'express' built in?", publicModules.includes("express"));
All of these are built in: true
Has fs/promises: true
Is 'express' built in? false

Packages like express come from npm and must be installed first.

Using a few built-ins together

Built-in modules are designed to cooperate. This small script uses path, util and crypto in a few lines:

node · try it yourself
import path from "node:path";
import { format } from "node:util";
import { createHash } from "node:crypto";

const file = path.join("reports", "2024", "sales.csv");
console.log(file.split(path.sep).join("/"));
console.log(path.extname(file));

console.log(format("%s has %d rows", "sales.csv", 120));

const hash = createHash("sha256").update("hello").digest("hex");
console.log(hash.slice(0, 16));
reports/2024/sales.csv
.csv
sales.csv has 120 rows
2cf24dba5fb0a30e

Callback, promise and sync flavours

Many older built-ins, especially fs, offer three styles of the same operation:

Style Example When to use
Promise await fsp.readFile("a.txt", "utf8") default choice for new code
Callback fs.readFile("a.txt", "utf8", (err, data) => {...}) older code bases
Sync fs.readFileSync("a.txt", "utf8") startup code and small CLI scripts
node · try it yourself
const fs = require("node:fs");
const fsp = require("node:fs/promises");

fs.writeFileSync("note.txt", "built-ins are great");

console.log("sync:", fs.readFileSync("note.txt", "utf8"));

fs.readFile("note.txt", "utf8", (err, text) => {
  console.log("callback:", text.length, "chars");

  fsp.readFile("note.txt", "utf8").then((t) => {
    console.log("promise:", t.toUpperCase());
    fs.unlinkSync("note.txt");
  });
});
sync: built-ins are great
callback: 19 chars
promise: BUILT-INS ARE GREAT

Note: Sync functions block the whole process until they finish. In a web server that means every other client waits, so prefer the promise API there.

Globals that are modules too

Some built-ins are so common that Node also exposes them as globals: Buffer, process, console, URL, URLSearchParams, TextEncoder, fetch, setTimeout and friends. You can still import them explicitly (import { Buffer } from "node:buffer"), which some teams prefer for clarity.

Tip: Some modules exist only with the prefix: node:test, node:sqlite and node:sea cannot be loaded as plain "test" or "sqlite".

Test yourself

What does the node: prefix in require("node:fs") mean?

  1. Load from npm
  2. Load the built-in core module
  3. Load a file named node
  4. Enable experimental features

Which module would you use to compute a SHA-256 hash?

  1. node:os
  2. node:util
  3. node:crypto
  4. node:zlib

Why should a web server avoid fs.readFileSync while handling requests?

  1. It returns the wrong data
  2. It blocks the event loop so other clients must wait
  3. It only works on Windows
  4. It is deprecated