Node.js - Overview

Node.js is an asynchronous, event-driven JavaScript runtime built on Chrome's V8 engine and libuv, designed to build scalable network applications.

Architecture & Event Loop

Node.js operates on a single-threaded event loop for JavaScript execution, offloading non-blocking I/O operations (file system, networking, DNS) to the system kernel or a thread pool via libuv.

  • Single-Threaded Event Loop: Handles client connections, executes JavaScript callbacks, and dispatches events.
  • Worker Threads (node:worker_threads): Native multi-threading for CPU-intensive computational tasks (image processing, cryptography, compression).
  • Cluster Module (node:cluster): Spawns multiple worker processes sharing server ports across CPU cores.

Modern Built-in Node.js Features (Node.js 20+)

Modern Node.js includes tools that previously required third-party packages:

Native File Watcher (node --watch)

Replaces nodemon without installing external dependencies:

node --watch app.js

Native Environment File Loading (--env-file)

Replaces the dotenv package:

node --env-file=.env server.js

Native Web APIs

  • fetch(): Built-in global fetch(), Request, Response, and Headers without node-fetch.
  • crypto: Global crypto.randomUUID() and Web Crypto API.
  • WebSocket: Standard browser-compatible WebSocket API available globally.

Native Test Runner (node:test)

Run unit tests directly without installing Jest or Mocha:

// math.test.js
import test from 'node:test';
import assert from 'node:assert/strict';

test('synchronous math addition', () => {
  assert.equal(1 + 2, 3);
});

Execute via CLI:

node --test

ES Modules (ESM) vs CommonJS (CJS)

Modern projects standardize on native ES Modules (import / export):

// package.json
{
  "type": "module"
}
  • When "type": "module" is set, all .js files are treated as ES Modules.
  • Use explicit file extensions for local imports: import { helper } from './helper.js'.
  • Built-in modules are prefixed with node: for security and fast resolution: import fs from 'node:fs/promises'.

When to Use Node.js

  • Ideal For: I/O-intensive network services, REST & GraphQL APIs, microservices, real-time websockets (chat, collaborative editing, gaming lobbies), BFF (Backend-for-Frontend) layers, and CLI tools.
  • Less Suitable For: Heavy CPU-bound synchronous computations (e.g. video transcoding or training machine learning models) unless offloaded to worker_threads or native C++/Rust addons via N-API.

Version Management with nvm

# Install and use the latest Long-Term Support (LTS) release
nvm install --lts
nvm use --lts

# Check active versions
node -v
npm -v
Recommended Reading
Sponsored
JavaScript: The Definitive Guide
The JS Bible
JavaScript: The Definitive Guide
By David FlanaganView on Amazon