Node.js Built-in Modules API Guide

June 20, 2026 · View on GitHub

SharpTS provides implementations of common Node.js built-in modules. This guide documents the supported APIs for TypeScript developers familiar with Node.js.

Import Syntax

All three import styles are supported:

// Default import (recommended for most modules)
import fs from 'fs';
import os from 'os';

// Named imports (for specific functions)
import { readFileSync, writeFileSync } from 'fs';
import { createHash, randomUUID } from 'crypto';

// Namespace import
import * as path from 'path';

// Mixed imports
import path, { join, resolve } from 'path';

assert

Assertion testing utilities for validating code behavior.

Methods

MethodSignatureDescription
okok(value, message?)Assert value is truthy
strictEqualstrictEqual(actual, expected, message?)Assert strict equality (===)
notStrictEqualnotStrictEqual(actual, expected, message?)Assert strict inequality (!==)
equalequal(actual, expected, message?)Assert loose equality (==)
notEqualnotEqual(actual, expected, message?)Assert loose inequality (!=)
deepStrictEqualdeepStrictEqual(actual, expected, message?)Assert deep strict equality
notDeepStrictEqualnotDeepStrictEqual(actual, expected, message?)Assert deep inequality
throwsthrows(fn, message?)Assert function throws
doesNotThrowdoesNotThrow(fn, message?)Assert function doesn't throw
failfail(message?)Always throws assertion error

Example

import { strictEqual, deepStrictEqual, throws } from 'assert';

strictEqual(1 + 1, 2);
strictEqual('hello'.length, 5);

deepStrictEqual({ a: 1, b: 2 }, { a: 1, b: 2 });

throws(() => {
  throw new Error('expected error');
});

AssertionError

All assertions throw AssertionError on failure with properties:

  • message - Error message
  • actual - Actual value
  • expected - Expected value
  • operator - Assertion operator name

child_process

Execute external processes and shell commands.

Methods

MethodSignatureDescription
execSyncexecSync(command, options?)Execute shell command synchronously
spawnSyncspawnSync(command, args?, options?)Spawn process synchronously
execFileSyncexecFileSync(file, args?, options?)Execute file synchronously without shell
execexec(command, options?, callback?)Execute shell command asynchronously
execFileexecFile(file, args?, options?, callback?)Execute file asynchronously without shell
spawnspawn(command, args?, options?)Spawn a child process, returns ChildProcess
forkfork(modulePath, args?, options?)Spawn a Node child with IPC channel

execSync Options

{
  cwd?: string,      // Working directory
  timeout?: number,  // Timeout in milliseconds
  env?: object       // Environment variables
}

spawnSync Options

{
  cwd?: string,    // Working directory
  shell?: boolean, // Run in shell
  env?: object     // Environment variables
}

spawnSync Return Value

{
  stdout: string,      // Standard output
  stderr: string,      // Standard error
  status: number|null, // Exit code (null on success)
  signal: string|null, // Signal if killed
  error: string|null   // Error message if failed
}

Example

import { execSync, spawnSync } from 'child_process';

// Execute shell command
const output = execSync('echo hello');
console.log(output); // "hello"

// Execute with options
const result = execSync('ls -la', { cwd: '/tmp' });

// Spawn process with arguments
const spawn = spawnSync('git', ['status'], { cwd: '/my/repo' });
console.log(spawn.stdout);

crypto

Cryptographic functions for hashing and random number generation.

Methods

MethodSignatureDescription
createHashcreateHash(algorithm)Create a Hash object
randomBytesrandomBytes(size)Generate secure random bytes
randomUUIDrandomUUID()Generate random UUID v4
randomIntrandomInt(max) or randomInt(min, max)Generate random integer

Supported Hash Algorithms

  • md5
  • sha1
  • sha256
  • sha384
  • sha512

Hash Object Methods

MethodSignatureDescription
updateupdate(data)Add data to hash (chainable)
digestdigest(encoding?)Finalize and return digest

Digest encodings: 'hex', 'base64', or omit for raw bytes.

Example

import { createHash, randomBytes, randomUUID, randomInt } from 'crypto';

// Create SHA-256 hash
const hash = createHash('sha256')
  .update('hello')
  .update('world')
  .digest('hex');
console.log(hash); // "936a185caaa266bb9cbe981e9e05cb78cd732b0b3280eb944412bb6f8f8f07af"

// Generate random bytes
const bytes = randomBytes(16);

// Generate UUID
const uuid = randomUUID();
console.log(uuid); // "550e8400-e29b-41d4-a716-446655440000"

// Random integers
const n = randomInt(100);        // 0-99
const m = randomInt(10, 20);     // 10-19

fs

File system operations. Synchronous, callback-style async, and promise-based APIs are all supported. For promise-based APIs see the fs/promises section below.

File Operations (sync)

MethodSignatureDescription
existsSyncexistsSync(path)Check if path exists
readFileSyncreadFileSync(path, encoding?)Read file contents
writeFileSyncwriteFileSync(path, data, encoding?)Write to file
appendFileSyncappendFileSync(path, data, encoding?)Append to file
copyFileSynccopyFileSync(src, dest)Copy file
renameSyncrenameSync(oldPath, newPath)Rename/move file
unlinkSyncunlinkSync(path)Delete file
truncateSynctruncateSync(path, len?)Truncate file to length
openSyncopenSync(path, flags, mode?)Open file, return fd
closeSynccloseSync(fd)Close file descriptor
readSyncreadSync(fd, buffer, offset, length, position?)Read from fd
writeSyncwriteSync(fd, buffer, ...)Write to fd
ftruncateSyncftruncateSync(fd, len?)Truncate via fd
fstatSyncfstatSync(fd)Stat via fd

Directory Operations (sync)

MethodSignatureDescription
mkdirSyncmkdirSync(path, options?)Create directory
mkdtempSyncmkdtempSync(prefix)Create unique temp directory
rmdirSyncrmdirSync(path, options?)Remove directory
readdirSyncreaddirSync(path, options?)List directory contents
opendirSyncopendirSync(path)Open directory handle

File Information / Permissions (sync)

MethodSignatureDescription
statSyncstatSync(path)Get file/directory stats
lstatSynclstatSync(path)Get stats (symlink-aware)
accessSyncaccessSync(path, mode?)Check file accessibility
realpathSyncrealpathSync(path)Resolve canonical path
readlinkSyncreadlinkSync(path)Read symlink target
symlinkSyncsymlinkSync(target, path, type?)Create symlink
linkSynclinkSync(existing, new)Create hard link
chmodSyncchmodSync(path, mode)Change permissions
chownSyncchownSync(path, uid, gid)Change ownership
lchownSynclchownSync(path, uid, gid)Change ownership (symlink-aware)
utimesSyncutimesSync(path, atime, mtime)Update access/modification times

Callback-style Async

Most file/directory sync methods have callback-style counterparts with the trailing (err, result) => … pattern: readFile, writeFile, appendFile, copyFile, rename, unlink, mkdir, readdir, stat, lstat, access, chmod, chown. Use fs/promises for Promise-returning equivalents.

Stat Object Properties

{
  isDirectory: boolean,
  isFile: boolean,
  size: number
}

rmdirSync Options

{
  recursive?: boolean  // Remove directory and contents
}

Example

import fs from 'fs';

// Read and write files
const content = fs.readFileSync('input.txt', 'utf8');
fs.writeFileSync('output.txt', content.toUpperCase());

// Check existence
if (fs.existsSync('config.json')) {
  const config = fs.readFileSync('config.json', 'utf8');
}

// Directory operations
fs.mkdirSync('new-folder');
const files = fs.readdirSync('.');
console.log(files);

// File stats
const stats = fs.statSync('myfile.txt');
if (stats.isFile) {
  console.log(`Size: ${stats.size} bytes`);
}

// Remove directory recursively
fs.rmdirSync('old-folder', { recursive: true });

Error Codes

Node.js-compatible error codes are thrown:

  • ENOENT - File/directory not found
  • EACCES - Permission denied
  • EEXIST - File already exists
  • EISDIR - Is a directory (expected file)
  • ENOTDIR - Not a directory
  • ENOTEMPTY - Directory not empty

os

Operating system information and utilities.

Methods

MethodSignatureDescription
platformplatform()Get OS platform
archarch()Get CPU architecture
hostnamehostname()Get machine hostname
homedirhomedir()Get user home directory
tmpdirtmpdir()Get temp directory path
typetype()Get OS type
releaserelease()Get OS release version
cpuscpus()Get CPU information
totalmemtotalmem()Get total system memory
freememfreemem()Get free system memory
userInfouserInfo()Get current user info

Properties

PropertyTypeDescription
EOLstringEnd-of-line character

Platform Values

  • 'win32' - Windows
  • 'linux' - Linux
  • 'darwin' - macOS

Architecture Values

  • 'x64' - 64-bit x86
  • 'ia32' - 32-bit x86
  • 'arm64' - 64-bit ARM
  • 'arm' - 32-bit ARM

cpus() Return Value

[
  { model: string, speed: number },
  // ...
]

userInfo() Return Value

{
  username: string,
  uid: number,
  gid: number,
  shell: string,
  homedir: string
}

Example

import os from 'os';

console.log(`Platform: ${os.platform()}`);  // "win32", "linux", "darwin"
console.log(`Architecture: ${os.arch()}`);  // "x64"
console.log(`Hostname: ${os.hostname()}`);
console.log(`Home: ${os.homedir()}`);
console.log(`Temp: ${os.tmpdir()}`);

// Memory info
const totalGB = os.totalmem() / (1024 * 1024 * 1024);
const freeGB = os.freemem() / (1024 * 1024 * 1024);
console.log(`Memory: ${freeGB.toFixed(1)}GB free of ${totalGB.toFixed(1)}GB`);

// CPU info
const cpus = os.cpus();
console.log(`CPUs: ${cpus.length} cores`);

path

File path manipulation utilities.

Methods

MethodSignatureDescription
joinjoin(...parts)Join path segments
resolveresolve(...parts)Resolve to absolute path
basenamebasename(path, ext?)Get filename
dirnamedirname(path)Get directory name
extnameextname(path)Get file extension
normalizenormalize(path)Normalize path
isAbsoluteisAbsolute(path)Check if path is absolute
relativerelative(from, to)Get relative path
parseparse(path)Parse path to components
formatformat(pathObj)Build path from components

Properties

PropertyTypeDescription
sepstringPath separator (/ or \\)
delimiterstringPath list delimiter (: or ;)

parse() Return Value

{
  root: string,  // "/" or "C:\\"
  dir: string,   // Directory path
  base: string,  // Filename with extension
  name: string,  // Filename without extension
  ext: string    // Extension including dot
}

Example

import path from 'path';

// Join paths
const fullPath = path.join('/users', 'john', 'documents', 'file.txt');
// "/users/john/documents/file.txt"

// Resolve to absolute
const absolute = path.resolve('./src', '../lib', 'utils.ts');

// Extract parts
console.log(path.dirname('/a/b/c.txt'));   // "/a/b"
console.log(path.basename('/a/b/c.txt'));  // "c.txt"
console.log(path.extname('/a/b/c.txt'));   // ".txt"

// Remove extension
console.log(path.basename('file.ts', '.ts')); // "file"

// Parse path
const parsed = path.parse('/home/user/file.txt');
// { root: "/", dir: "/home/user", base: "file.txt", name: "file", ext: ".txt" }

// Build path
const built = path.format({ dir: '/home/user', base: 'file.txt' });
// "/home/user/file.txt"

// Relative path
console.log(path.relative('/a/b/c', '/a/d/e')); // "../../d/e"

process

Process information and control.

Properties

PropertyTypeDescription
platformstringOS platform
archstringCPU architecture
pidnumberProcess ID
versionstringNode.js version string
envobjectEnvironment variables
argvstring[]Command-line arguments
exitCodenumberCurrent exit code
stdinStreamStandard input
stdoutStreamStandard output
stderrStreamStandard error

Methods

MethodSignatureDescription
cwdcwd()Get current working directory
chdirchdir(path)Change working directory
exitexit(code?)Exit process
hrtimehrtime(time?)High-resolution time
uptimeuptime()Process uptime in seconds
memoryUsagememoryUsage()Memory usage statistics

Example

import process from 'process';

// Environment
console.log(`Platform: ${process.platform}`);
console.log(`PID: ${process.pid}`);
console.log(`CWD: ${process.cwd()}`);

// Environment variables
const home = process.env.HOME || process.env.USERPROFILE;
console.log(`Home: ${home}`);

// Command-line arguments
process.argv.forEach((arg, index) => {
  console.log(`argv[${index}]: ${arg}`);
});

// Change directory
process.chdir('/tmp');

// Timing
const start = process.hrtime();
// ... some operation ...
const elapsed = process.hrtime(start);
console.log(`Took ${elapsed[0]}s ${elapsed[1]}ns`);

// Memory
const mem = process.memoryUsage();
console.log(`Heap used: ${mem.heapUsed}`);

querystring

URL query string parsing and serialization.

Methods

MethodSignatureDescription
parseparse(str, sep?, eq?, options?)Parse query string to object
stringifystringify(obj, sep?, eq?, options?)Convert object to query string
escapeescape(str)Percent-encode string
unescapeunescape(str)Percent-decode string
decode-Alias for parse
encode-Alias for stringify

Example

import querystring from 'querystring';

// Parse query string
const parsed = querystring.parse('name=john&age=30&hobby=coding&hobby=gaming');
// { name: "john", age: "30", hobby: ["coding", "gaming"] }

// Stringify object
const qs = querystring.stringify({ name: 'john', tags: ['a', 'b'] });
// "name=john&tags=a&tags=b"

// Custom separators
const custom = querystring.parse('name:john;age:30', ';', ':');
// { name: "john", age: "30" }

// Escape/unescape
const escaped = querystring.escape('hello world');  // "hello%20world"
const decoded = querystring.unescape('hello%20world'); // "hello world"

readline

User input handling for interactive applications.

Methods

MethodSignatureDescription
questionSyncquestionSync(query)Prompt user synchronously
createInterfacecreateInterface(options?)Create readline interface

Interface Methods

MethodSignatureDescription
questionquestion(query, callback)Ask question with callback
closeclose()Close the interface
promptprompt()Display prompt character

Example

import readline from 'readline';

// Simple synchronous prompt
const name = readline.questionSync('What is your name? ');
console.log(`Hello, ${name}!`);

// Using interface
const rl = readline.createInterface();

rl.question('Enter a number: ', (answer) => {
  console.log(`You entered: ${answer}`);
  rl.close();
});

url

URL parsing and manipulation.

Classes

URL (WHATWG URL API)

new URL(urlString, baseUrl?)

Properties:

  • href - Full URL string
  • protocol - Protocol with colon (e.g., 'https:')
  • host - Host with port
  • hostname - Host without port
  • port - Port number as string
  • pathname - Path portion
  • search - Query string with ?
  • hash - Fragment with #
  • origin - Protocol + host
  • username - Username portion
  • password - Password portion
  • searchParams - URLSearchParams object

URLSearchParams

new URLSearchParams(init?)

Methods:

  • get(name) - Get first value for name
  • getAll(name) - Get all values for name
  • has(name) - Check if name exists
  • set(name, value) - Set value (replaces existing)
  • append(name, value) - Append value
  • delete(name) - Remove all values for name
  • keys() - Get all keys
  • values() - Get all values
  • size - Number of parameters

Legacy Functions

FunctionSignatureDescription
parseparse(urlString, parseQueryString?, slashesDenoteHost?)Parse URL string
formatformat(urlObject)Format URL object to string
resolveresolve(from, to)Resolve relative URL

Example

import { URL, URLSearchParams } from 'url';

// Parse URL
const url = new URL('https://example.com:8080/path?query=value#hash');
console.log(url.hostname);  // "example.com"
console.log(url.port);      // "8080"
console.log(url.pathname);  // "/path"
console.log(url.search);    // "?query=value"

// Modify URL
url.pathname = '/new-path';
url.searchParams.set('foo', 'bar');
console.log(url.href);

// URLSearchParams
const params = new URLSearchParams('a=1&b=2&a=3');
console.log(params.get('a'));     // "1"
console.log(params.getAll('a'));  // ["1", "3"]
params.append('c', '4');
params.delete('b');

// Resolve relative URLs
import { resolve } from 'url';
const absolute = resolve('https://example.com/a/b', '../c');
// "https://example.com/a/c"

util

Utility functions for formatting and type checking.

Methods

MethodSignatureDescription
formatformat(format, ...args)Format string with placeholders
inspectinspect(value, options?)Convert value to string representation

format() Placeholders

PlaceholderDescription
%sString
%d, %iInteger
%fFloat
%jJSON
%o, %OObject
%%Literal %

inspect() Options

{
  depth?: number  // Recursion depth (default: 2)
}

types Object

Type checking utilities:

MethodSignatureDescription
isArrayisArray(value)Check if array
isDateisDate(value)Check if Date
isFunctionisFunction(value)Check if function
isNullisNull(value)Check if null
isUndefinedisUndefined(value)Check if undefined

Example

import util from 'util';

// Format strings
const msg = util.format('Hello %s, you have %d messages', 'John', 5);
// "Hello John, you have 5 messages"

const json = util.format('Data: %j', { a: 1, b: 2 });
// "Data: {\"a\":1,\"b\":2}"

// Inspect objects
const obj = { nested: { deep: { value: 42 } } };
console.log(util.inspect(obj, { depth: 1 }));
// "{ nested: { deep: [Object] } }"

// Type checking
console.log(util.types.isArray([1, 2, 3]));  // true
console.log(util.types.isFunction(() => {})); // true
console.log(util.types.isNull(null));         // true

fs/promises

Promise-based counterparts of the fs callback API. Every listed method returns a Promise and accepts the same arguments as the synchronous form (minus the error-first callback).

MethodDescription
readFileRead file contents
writeFileWrite file contents
appendFileAppend to file
copyFileCopy file
renameRename/move
unlinkDelete file
truncateTruncate to length
mkdirCreate directory
mkdtempCreate temp directory
rmdir / rmRemove directory/file
readdirList directory contents
stat / lstatFile info
accessCheck accessibility
chmodChange permissions
realpathResolve canonical path
readlink / symlink / linkSymlink / hardlink ops
utimesUpdate atime/mtime
openReturns a FileHandle

Example

import { readFile, writeFile } from 'fs/promises';

const content = await readFile('input.txt', 'utf8');
await writeFile('output.txt', content.toUpperCase());

buffer

Binary data handling via the Buffer class. Buffer is also exposed as a global — import is only required when the consumer wants the named binding.

Static Methods

MethodSignatureDescription
fromBuffer.from(source, encoding?)Create from string, array, or ArrayBuffer
allocBuffer.alloc(size, fill?, encoding?)Allocate zero-filled buffer
allocUnsafeBuffer.allocUnsafe(size)Allocate without zeroing
allocUnsafeSlowBuffer.allocUnsafeSlow(size)Allocate without the pool
isBufferBuffer.isBuffer(obj)Type guard
isEncodingBuffer.isEncoding(encoding)Encoding supported?
byteLengthBuffer.byteLength(value, encoding?)Byte length of a string
concatBuffer.concat(list, totalLength?)Concatenate buffers
compareBuffer.compare(a, b)Sort-compatible compare

Instance Methods

toString, write, slice, equals, compare, copy, fill, indexOf, includes, toJSON, swap16, swap32, swap64. Typed read/write: readInt8, readUInt8, readInt16BE/LE, readUInt16BE/LE, readInt32BE/LE, readUInt32BE/LE, readFloatBE/LE, readDoubleBE/LE, readBigInt64BE/LE, readBigUInt64BE/LE, and the matching write* variants.

Supported Encodings

utf8, utf16le/ucs2, ascii, latin1/binary, base64, hex.

Example

const buf = Buffer.from('hello', 'utf8');
console.log(buf.length);              // 5
console.log(buf.toString('hex'));     // "68656c6c6f"
const merged = Buffer.concat([buf, Buffer.from(' world')]);

events

Event-driven programming via EventEmitter.

EventEmitter

MethodDescription
on(name, listener) / addListenerSubscribe
once(name, listener)Subscribe for a single fire
prependListener / prependOnceListenerSubscribe at the front of the queue
off(name, listener) / removeListenerUnsubscribe
removeAllListeners(name?)Remove all listeners for an event (or everything)
emit(name, ...args)Dispatch to listeners — returns true if any ran
listenerCount(name)Number of subscribers
eventNames()Names with at least one listener
setMaxListeners(n)Warn threshold

Example

import { EventEmitter } from 'events';

const ee = new EventEmitter();
ee.on('tick', (n: number) => console.log(`tick ${n}`));
ee.emit('tick', 1);

stream

Node.js streaming primitives.

Exports

Classes: Readable, Writable, Duplex, Transform, PassThrough.

Helpers: pipeline(...streams, cb?), finished(stream, cb), addAbortSignal(signal, stream).

The submodule stream/promises exposes promise-returning pipeline and finished.

stream/web

WHATWG streams: ReadableStream, WritableStream, TransformStream, ByteLengthQueuingStrategy, CountQueuingStrategy.

Example

import { pipeline } from 'stream/promises';
import { createReadStream, createWriteStream } from 'fs';
import { createGzip } from 'zlib';

await pipeline(
  createReadStream('input.txt'),
  createGzip(),
  createWriteStream('input.txt.gz'),
);

http / https

HTTP client and server.

Methods

MethodDescription
createServer(requestListener?)Create an HTTP server
request(options, callback?)Make an HTTP request
get(options, callback?)request with method forced to GET

Constants / Objects

  • METHODS — array of supported HTTP method names
  • STATUS_CODES{ code: reason-phrase } map
  • Agent — connection pooling
  • globalAgent — default shared agent

https exposes the same API with TLS transport.

Example

import http from 'http';

const server = http.createServer((req, res) => {
  res.writeHead(200, { 'Content-Type': 'text/plain' });
  res.end('Hello\n');
});
server.listen(3000);

net

TCP networking.

SymbolDescription
createServer(options?, connectionListener?)New TCP server
createConnection(options, callback?) / connectNew TCP connection
ServerTCP server class
SocketTCP socket class
isIP(input)4, 6, or 0
isIPv4 / isIPv6Version-specific checks

Example

import net from 'net';

const server = net.createServer((socket) => {
  socket.write('hello\n');
  socket.end();
});
server.listen(7000);

tls

TLS/SSL sockets on top of net.

SymbolDescription
createServer(options, connectionListener?)TLS server
connect(options, callback?)Client connection
createSecureContext(options)Reusable credential bundle
Server, TLSSocketClasses
DEFAULT_MIN_VERSION, DEFAULT_MAX_VERSIONDefault protocol bounds

Options accept key, cert, ca, host, port, minVersion, maxVersion.


dgram

UDP datagrams.

SymbolDescription
createSocket(type, callback?)Create a socket ('udp4' or 'udp6')
SocketSocket class — bind, send, close, EventEmitter interface

Example

import dgram from 'dgram';

const sock = dgram.createSocket('udp4');
sock.on('message', (msg, rinfo) => console.log(`from ${rinfo.address}: ${msg}`));
sock.bind(41234);

dns / dns/promises

DNS resolution.

resolve* vs lookup: resolve/resolve4/resolve6/resolveMx/… use the DNS wire protocol — like Node's c-ares resolver — querying the configured DNS server (override with the SHARPTS_DNS_SERVER env var). They do not consult the OS hosts file. lookup/lookupService use the OS resolver (getaddrinfo), which does read the hosts file. Consequently resolve4('localhost') typically returns ENOTFOUND, whereas lookup('localhost') returns 127.0.0.1 — matching Node.

Top-level Methods

lookup, lookupService, resolve, resolve4, resolve6, resolveCaa, resolveCname, resolveMx, resolveNs, resolvePtr, resolveSoa, resolveSrv, resolveTxt, resolveAny, reverse, getServers, setServers.

Resolver

Resolver class provides a private instance with its own servers: new Resolver(), resolver.resolve4(host, cb), resolver.setServers([...]), resolver.cancel().

Promise API

import { resolve4 } from 'dns/promises' — same surface, Promise-returning. Also available as dns.promises.

Error Codes

Exposed as named constants: NOTFOUND, SERVFAIL, REFUSED, TIMEOUT, NODATA, FORMERR, NOMEM, BADQUERY, BADNAME, BADFAMILY, BADRESP, CONNREFUSED, CANCELLED, and more.


zlib

Compression/decompression.

Sync

deflateSync, inflateSync, deflateRawSync, inflateRawSync, gzipSync, gunzipSync, unzipSync, brotliCompressSync, brotliDecompressSync.

Async (callback)

deflate, inflate, deflateRaw, inflateRaw, gzip, gunzip, unzip, brotliCompress, brotliDecompress.

Stream Factories

createDeflate, createInflate, createDeflateRaw, createInflateRaw, createGzip, createGunzip, createUnzip, createBrotliCompress, createBrotliDecompress, createZstdCompress, createZstdDecompress.

Example

import { gzipSync, gunzipSync } from 'zlib';

const compressed = gzipSync(Buffer.from('hello world'));
const original = gunzipSync(compressed).toString();

timers / timers/promises

Timer functions. Also available as globals.

MethodDescription
setTimeout(cb, ms, ...args)Run once after delay
setInterval(cb, ms, ...args)Repeat every delay
setImmediate(cb, ...args)Run on next tick
clearTimeout(handle) / clearInterval / clearImmediateCancel

timers/promises exposes setTimeout(ms, value?, options?) and setImmediate(value?, options?) returning a Promise, both accepting { signal } for cancellation via AbortController.

Example

import { setTimeout as delay } from 'timers/promises';

await delay(1000);
console.log('one second later');

vm

Compile and execute code in a custom context.

MethodDescription
createContext(contextObject?)Wrap an object as a sandbox
isContext(obj)Test for a context object
runInContext(code, context, options?)Run code in an existing context
runInNewContext(code, contextObject?, options?)Create a context and run
runInThisContext(code, options?)Run in the current context
compileFunction(code, params?, options?)Compile to a callable function
Scriptnew Script(code, options?) with .runInContext() / .runInNewContext() / .runInThisContext()

Options include timeout for execution cap.


worker_threads

Thread-based workers.

SymbolDescription
WorkerSpawn a worker; postMessage, terminate, on('message')
MessageChannel, BroadcastChannelStructured messaging primitives
isMainThreadtrue in the main thread
parentPortMessage port to parent (in workers)
workerDataData passed at construction
threadIdCurrent thread ID
resourceLimitsPer-worker limits
getEnvironmentData / setEnvironmentDataShared env map
SHARE_ENVSentinel to inherit environment
markAsUntransferable / moveMessagePortToContext / receiveMessageOnPortAdvanced port ops

cluster

Fork worker processes that share server ports.

SymbolDescription
fork(env?)Create a worker
disconnect(callback?)Gracefully disconnect all workers
isPrimary / isMastertrue in the primary/master process
isWorkertrue in worker processes
workerCurrent worker (when isWorker)
workers{ id: Worker } map (primary)
settingsActive fork settings
setupPrimary(settings?) / setupMasterConfigure default fork settings

Extends EventEmitter (on, once, emit, off, removeAllListeners, listenerCount, listeners, eventNames).


async_hooks

Asynchronous context tracking.

AsyncLocalStorage

MethodDescription
run(store, callback, ...args)Execute within a store scope
enterWith(store)Set store for the current async chain
exit(callback, ...args)Run without any store
getStore()Current store, or undefined
disable()Drop all references

Example

import { AsyncLocalStorage } from 'async_hooks';

const als = new AsyncLocalStorage<{ requestId: string }>();

als.run({ requestId: 'abc' }, () => {
  // Any `getStore()` call in this (and descendant) async task sees { requestId: 'abc' }
  handleRequest();
});

perf_hooks

High-resolution performance timing.

performance

MemberDescription
now()Monotonic time in ms since timeOrigin
timeOriginWall-clock anchor (ms since epoch)
mark(name, options?)Record a named timestamp
measure(name, startMark?, endMark?)Record a duration between marks
getEntries() / getEntriesByName / getEntriesByTypeQuery recorded entries
clearMarks(name?) / clearMeasures(name?)Drop entries

PerformanceObserver

Subscribe to mark/measure events synchronously:

import { performance, PerformanceObserver } from 'perf_hooks';

const obs = new PerformanceObserver((list) => {
  for (const entry of list.getEntries()) console.log(entry.name, entry.duration);
});
obs.observe({ entryTypes: ['measure'] });

performance.mark('start');
// ... work ...
performance.mark('end');
performance.measure('work', 'start', 'end');

string_decoder

Incremental decoding of Buffer chunks to strings, preserving multi-byte sequences across writes.

StringDecoder

new StringDecoder(encoding?)  // default 'utf8'
MethodDescription
write(buffer)Decode chunk, buffering any trailing incomplete sequence
end(buffer?)Flush any buffered bytes and return the final string

Example

import { StringDecoder } from 'string_decoder';

const decoder = new StringDecoder('utf8');
const a = decoder.write(Buffer.from([0xE2, 0x82]));       // "" — incomplete
const b = decoder.write(Buffer.from([0xAC]));             // "€"

tty

Terminal detection.

MethodDescription
isatty(fd)Is the file descriptor a TTY?

ReadStream / WriteStream classes are not currently implemented — use process.stdout.isTTY (a boolean) or tty.isatty(1) for the common case.


Notes

Error Handling

File system errors include Node.js-compatible error codes:

try {
  fs.readFileSync('nonexistent.txt');
} catch (e) {
  if (e.code === 'ENOENT') {
    console.log('File not found');
  }
}

Stream Objects

The process.stdin, process.stdout, and process.stderr properties are stream objects with standard stream methods.