Node.js Core Modules
Node.js core modules cheat sheet covering fs, http, path, crypto, streams, events, and process with practical code examples.
Other Node.js Sheets
Sign in to mark items as known and track your progress.
Sign inFile System (fs)
Read, write, and manipulate files and directories
File Operations
Read, write, delete files with async and sync methods
// Read file
const data = fs.readFileSync('file.txt', 'utf8');
fs.readFile('file.txt', 'utf8', (err, data) => {});
// Write file
fs.writeFileSync('file.txt', 'content');
fs.appendFileSync('file.txt', 'more');
// Delete/Check
fs.unlinkSync('file.txt');
fs.existsSync('file.txt');Path Module
Work with file and directory paths across platforms
const path = require('path');
// Join paths
path.join('/users', 'john', 'documents', 'file.txt');
// Get parts
path.dirname('/users/john/file.txt'); // /users/john
path.basename('/users/john/file.txt'); // file.txt
path.extname('file.txt'); // .txt
// Resolve absolute
path.resolve('file.txt');Directory Operations
Create, read, and remove directories
const fs = require('fs').promises
const path = require('path')
// Create directory (recursive)
await fs.mkdir('path/to/dir', { recursive: true })
// Read directory contents
const files = await fs.readdir('./src')
// With file types
const entries = await fs.readdir('./src', { withFileTypes: true })
const dirs = entries.filter(e => e.isDirectory())
// Remove directory
await fs.rmdir('path/to/dir')
// Remove with contents (Node 14+)
await fs.rm('path/to/dir', { recursive: true, force: true })HTTP & HTTPS
Create servers and make HTTP requests
HTTP Server
Create HTTP servers to handle web requests
const http = require('http');
// Create server
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello World');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});HTTP Client Requests
Make HTTP requests to external APIs and services
const http = require('http');
// GET request
http.get('http://api.example.com/data', (res) => {
let data = '';
res.on('data', chunk => data += chunk);
res.on('end', () => console.log(data));
});
// POST request
const req = http.request(options, (res) => {
// Handle response
});
req.write(JSON.stringify(data));
req.end();Process & OS
Interact with system processes and operating system
Process Module
Access command line arguments, environment, and control process
// Arguments & Environment
console.log(process.argv);
console.log(process.env.NODE_ENV);
// Exit
process.exit(0);
// Current directory
process.cwd();
// Memory usage
process.memoryUsage();
// Event handlers
process.on('uncaughtException', (err) => {
console.error(err);
});OS Module
Get operating system information and utilities
const os = require('os');
// System info
os.platform(); // 'darwin', 'linux', 'win32'
os.arch(); // 'x64', 'arm'
os.hostname();
os.cpus();
os.totalmem();
os.freemem();
// User info
os.userInfo();
os.homedir();Child Process
Execute system commands and spawn new processes
const { spawn, exec, execSync } = require('child_process')
// Execute simple command (callback-based)
exec('ls -la', (error, stdout, stderr) => {
if (error) console.error(\`Error: \${error}\`)
console.log(stdout)
})
// Synchronous execution (blocks)
const result = execSync('npm list', { encoding: 'utf-8' })
console.log(result)
// Spawn for streaming/long-running
const ls = spawn('ls', ['-la', '/usr'])
ls.stdout.on('data', (data) => {
console.log(\`Output: \${data}\`)
})Crypto & Buffer
Cryptography operations and binary data handling
Crypto Module
Cryptographic functionality for hashing, encryption, and security
const crypto = require('crypto');
// Hash
const hash = crypto.createHash('sha256')
.update('password')
.digest('hex');
// Random
const token = crypto.randomBytes(32).toString('hex');
// Encrypt/Decrypt
const cipher = crypto.createCipher('aes192', 'password');
const encrypted = cipher.update('text', 'utf8', 'hex');Buffer Module
Handle binary data and convert between encodings
// Create buffers
const buf1 = Buffer.alloc(10);
const buf2 = Buffer.from('Hello');
const buf3 = Buffer.from([1, 2, 3]);
// Read/Write
buf1.write('Hello');
console.log(buf1.toString());
// Compare
Buffer.compare(buf1, buf2);Streams & Events
Handle streaming data and event-driven programming
Streams
Process data piece by piece without loading all into memory
const fs = require('fs');
// Readable stream
const readable = fs.createReadStream('input.txt');
readable.on('data', chunk => console.log(chunk));
// Writable stream
const writable = fs.createWriteStream('output.txt');
writable.write('Hello');
// Pipe
readable.pipe(writable);Event Emitter
Create and handle custom events for async communication
const EventEmitter = require('events');
class MyEmitter extends EventEmitter {}
const emitter = new MyEmitter();
// Listen for event
emitter.on('event', (data) => {
console.log('Event:', data);
});
// Emit event
emitter.emit('event', 'data');
// Once listener
emitter.once('close', () => {});Utilities & Advanced
Utility functions and advanced Node.js features
URL & QueryString
Parse and construct URLs and query strings
const { URL } = require('url')
const querystring = require('querystring')
// Modern URL API (recommended)
const myUrl = new URL('https://example.com:8000/path?name=John&age=30')
console.log({
hostname: myUrl.hostname,
pathname: myUrl.pathname,
searchParams: myUrl.searchParams.get('name')
})
// Modify URL
myUrl.searchParams.append('role', 'admin')
myUrl.pathname = '/users/profile'
console.log(myUrl.href)
// Parse query string
const query = querystring.parse('name=John&age=30&hobbies=coding&hobbies=music')
// Result: { name: 'John', age: '30', hobbies: ['coding', 'music'] }Util Module
Utility functions for debugging and promisification
const util = require('util')
const fs = require('fs')
// Promisify callback-based functions
const readFile = util.promisify(fs.readFile)
const data = await readFile('file.txt', 'utf8')
// Format strings (like printf)
const msg = util.format('%s:%d', 'Server', 8080)
// 'Server:8080'
// Deep inspect objects
const obj = { a: 1, b: { c: 2, d: [3, 4] } }
console.log(util.inspect(obj, {
showHidden: true,
depth: null,
colors: true
}))Worker Threads
Run JavaScript in parallel threads for CPU-intensive tasks
const { Worker, isMainThread, parentPort, workerData } = require('worker_threads')
if (isMainThread) {
// Main thread code
const worker = new Worker(__filename, {
workerData: { iterations: 1000000 }
})
worker.on('message', (result) => {
console.log('Result:', result)
})
worker.on('error', (err) => {
console.error('Worker error:', err)
})
worker.postMessage({ cmd: 'START' })
} else {
// Worker thread code
parentPort.on('message', (msg) => {
if (msg.cmd === 'START') {
// CPU-intensive task
let result = 0
for (let i = 0; i < workerData.iterations; i++) {
result += Math.sqrt(i)
}
parentPort.postMessage(result)
}
})
}Cluster Module
Scale Node.js apps across multiple CPU cores
const cluster = require('cluster')
const http = require('http')
const os = require('os')
if (cluster.isMaster) {
const cpuCount = os.cpus().length
console.log(\`Master \${process.pid} spawning \${cpuCount} workers\`)
// Fork workers
for (let i = 0; i < cpuCount; i++) {
cluster.fork()
}
cluster.on('exit', (worker, code, signal) => {
console.log(\`Worker \${worker.process.pid} died\`)
console.log('Starting new worker...')
cluster.fork()
})
} else {
// Workers share server port
http.createServer((req, res) => {
res.writeHead(200)
res.end(\`Worker \${process.pid} responded\\n\`)
}).listen(8000)
console.log(\`Worker \${process.pid} started\`)
}Modern Node.js (Built-in)
Built-in APIs added to recent Node.js LTS releases
node: Import Prefix
Explicitly import built-in modules to avoid name collisions
// Prefer the node: prefix for built-in modules (Node 16+)
import fs from 'node:fs/promises'
import path from 'node:path'
import { createServer } from 'node:http'
import { fileURLToPath } from 'node:url'
import { setTimeout as sleep } from 'node:timers/promises'
// Why: the prefix guarantees you get the built-in, not a same-named npm
// package, and makes intent obvious to humans and tools.Built-in fetch & Web Streams
fetch, Request, Response, Headers, FormData, AbortController — all global
// Built-in fetch (stable since Node 21) — no node-fetch needed
const res = await fetch('https://api.example.com/users')
if (!res.ok) throw new Error('HTTP ' + res.status)
const users = await res.json()
// Streaming a response
const res = await fetch('https://example.com/big.csv')
for await (const chunk of res.body) {
process.stdout.write(chunk)
}
// Aborting a slow request
const ac = new AbortController()
const timer = setTimeout(() => ac.abort(), 5000)
try {
const r = await fetch(url, { signal: ac.signal })
} finally {
clearTimeout(timer)
}node:test Runner
Built-in test runner with describe / it / assert — no Jest needed
import { test, describe, it, before, after, mock } from 'node:test'
import assert from 'node:assert/strict'
test('adds two numbers', () => {
assert.equal(1 + 1, 2)
})
describe('User', () => {
it('starts inactive', () => {
const u = new User()
assert.equal(u.active, false)
})
})
// Run with: node --test
// Run a single file: node --test test/user.test.js
// Watch mode: node --test --watch