Node.js Cheat Sheet
|
This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production. |
A single-page, colour-coded summary of the Node.js essentials, written against Node.js 26 (26.10.0, Current; it becomes LTS on 2026-10-28), with the differences of Node.js 24 LTS (24.21.0) and Node.js 22 (22.23.3, Maintenance) called out, and npm 11 as bundled with those lines (versions verified 2026-10-03) — what the runtime is made of, the CLI and REPL, CommonJS and ES modules, npm, the event loop, promises and events, process and error handling, files and streams, built-in SQLite, HTTP and networking, concurrency, the test runner, diagnostics, security, TypeScript type stripping, production, and a strip of what changed since older tutorials — handy for quick memorization or as a downloadable PDF desk reference.
For the full explanation, every option, and additional examples, see the dedicated pages:
Start here — Node.js and its Bibliography.
Foundations — Introduction & Architecture, Installing Node.js, Release Lines & LTS, Running Scripts, the CLI & the REPL, CLI Options, Environment & Config File, and Globals & Web Platform APIs.
Modules & packages — CommonJS Modules, ECMAScript Modules, ESM & CommonJS Interop, package.json, exports & imports, npm & Package Management, and Workspaces & Publishing.
Asynchrony — The Event Loop, Callbacks, Promises & async/await, Timers, Events & EventEmitter, Don’t Block the Event Loop, and Async Context & Cancellation.
Core APIs — Process, Signals & Exit Codes, Errors & Error Handling, Buffers & Binary Data, The util Module, os, path & URL, and Console, readline & Terminal I/O.
Files, streams & data — The File System, Directories, Glob & Watch, Streams, Implementing Custom Streams, Web Streams & Interop, Compression with zlib, Built-in SQLite, and Databases from Node.js.
Networking & HTTP — HTTP Server Fundamentals, HTTPS, TLS & HTTP/2, fetch & HTTP Clients, WebSockets & Server-Sent Events, TCP, UDP & DNS, and Server Timeouts, Limits & Hardening.
Concurrency & scaling — Concurrency Models, Worker Threads, Child Processes, and Cluster & Scaling.
Testing — The Test Runner, and Mocking, Snapshots & Coverage.
Debugging & diagnostics — Debugging, Profiling & Performance, Memory & Leaks, and Diagnostic Reports, Logging & Observability.
Security — Security Best Practices, The Permission Model, and Crypto, Passwords & Secrets.
TypeScript & tooling — TypeScript in Node.js, and Linting, Formatting & Tooling.
Building applications — Building a REST API, Web Frameworks: Express & Fastify, Authentication & Sessions, Building CLI Tools, Background Jobs, Scheduling & Queues, Email, Feeds & Web Scraping, and Integrating LLM APIs.
Production — Production Deployment, Docker & Containers, Single Executable Applications, and Upgrading & What’s Changed.
Beyond the basics — Native Add-ons, WebAssembly & FFI.
Worked example
One small ES module that exercises four of the essentials above: concurrent promises, error codes from a failed file read, a stream pipeline and a one-shot event listener.
Save it as cheat.mjs and run it with node cheat.mjs.
import { EventEmitter } from 'node:events';
import { readFile } from 'node:fs/promises';
import { setTimeout as sleep } from 'node:timers/promises';
import { pipeline } from 'node:stream/promises';
import { Readable, Transform } from 'node:stream';
// Promises: two 100 ms waits run concurrently with Promise.all
const started = performance.now();
const results = await Promise.all([sleep(100, 'a'), sleep(100, 'b')]);
const elapsed = performance.now() - started;
console.log('results:', results, 'concurrent:', elapsed < 190);
// Errors: a failed read rejects with a code
try {
await readFile(new URL('./missing.txt', import.meta.url));
} catch (err) {
console.log('caught:', err.code);
}
// Streams: pipeline handles backpressure and cleanup
const upper = new Transform({
transform(chunk, _enc, cb) { cb(null, chunk.toString().toUpperCase()); },
});
let out = '';
await pipeline(
Readable.from(['node', ' ', 'streams']),
upper,
async function (source) { for await (const c of source) out += c; },
);
// Events: a one-shot listener
const bus = new EventEmitter();
bus.once('done', (text) => console.log('event done:', text));
bus.emit('done', out);
console.log('listeners left:', bus.listenerCount('done'));
Output on Node.js 24 (the program uses only APIs that exist on 22 and 26 as well):
results: [ 'a', 'b' ] concurrent: true
caught: ENOENT
event done: NODE STREAMS
listeners left: 0
The two 100 ms waits finish together because Promise.all() starts both before awaiting either, the missing file surfaces as a rejection carrying err.code, pipeline() closes every stream in the chain when it ends, and once() removes its listener after the first call.