Chapter 21: Core Modules

1. What are Core Modules?

Core modules (also called built-in modules) are modules that come pre-installed with every Node.js installation.

You do not need to run npm install to use them — they are part of Node.js itself.

You only need to import or require them.

Most important characteristics (2025–2026):

  • They are very stable (almost never break with new Node versions)
  • They are optimized and written in C++ under the hood
  • Many of them now have promise-based APIs (node:fs/promises, node:timers/promises, etc.)
  • All modern code should use the node: prefix when importing them → import fs from ‘node:fs/promises’ (recommended style)

2. The most important core modules (with real usage examples)

I’ll list the ones you will use most often in real projects, ordered by how frequently they appear in typical applications.

1. node:path – File & directory paths

Why it’s important: Never concatenate paths manually (folder + ‘/’ + file) — it breaks on Windows vs Linux/macOS.

JavaScript

Very common pattern:

JavaScript

2. node:fs & node:fs/promises – File system

Most used module in backend development

JavaScript

Common operations 2026 style:

JavaScript

3. node:http & node:https – Low-level HTTP server & client

Most people use Express / Fastify / Hono, but understanding the core is useful.

Minimal HTTP server (classic example)

JavaScript

Modern fetch-style client (Node.js 18+)

JavaScript

4. node:os – Operating system information

JavaScript

Very useful pattern: Decide behavior based on OS

JavaScript

5. node:process – Information & control of current Node process

JavaScript

6. node:events – Event emitter (foundation of many APIs)

JavaScript

Many core modules inherit from EventEmitter:

  • http.Server
  • stream.Readable / Writable
  • net.Server / Socket
  • child_process.ChildProcess

7. Modern pattern – using the node: prefix (best practice 2025–2026)

JavaScript

Why node: prefix?

  • Makes it clear it’s a core module (not from npm)
  • Prevents name collisions if someone publishes an npm package called fs
  • Better IDE auto-complete & documentation

8. Quick reference table – most used core modules

Module Promise-based version Main use cases Frequency in real projects
node:path Path manipulation, __dirname, join, resolve ★★★★★
node:fs/promises Yes Read/write files, mkdir, readdir, access ★★★★★
node:process env, argv, cwd, exit, pid, platform ★★★★☆
node:os CPU count, memory, platform, homedir ★★★☆☆
node:http Create raw HTTP servers (rarely used directly) ★★☆☆☆
node:events Custom events, EventEmitter ★★★★☆
node:stream Streaming data (files, http, compression) ★★★☆☆
node:child_process Run external commands, spawn processes ★★☆☆☆
node:crypto Yes (webcrypto) Hashing, encryption, random bytes ★★★☆☆
node:util Yes (promisify) promisify, inspect, format ★★☆☆☆

Summary – Quick mental checklist

  • Use node: prefix in imports
  • Prefer promise-based versions (fs/promises, timers/promises, etc.)
  • Use path.join / path.resolve — never string concatenation for paths
  • process.env is your main way to read configuration
  • fs.promises is the standard way to read/write files in modern code
  • EventEmitter is the foundation of almost all async APIs in Node

Which core module or pattern would you like to explore much deeper next?

  • Deep dive into node:stream (very powerful but tricky)
  • Using node:child_process in real projects
  • node:crypto – hashing, JWT signing, random tokens
  • node:worker_threads – escaping single-thread limitations
  • Complete example project using 8–10 core modules together

Just tell me what interests you most — I’ll continue with concrete, copy-paste-ready examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *