Modern Node.js
Selection card
- Task: Node runtime APIs, ESM and async cancellation.
- Start: Read package.json (type, engines, exports, scripts) and the Node version the project actually runs on.
- Finish: The code runs on the target Node version with cancellation, errors and cleanup handled, and type checks and tests pass.
Overview
Node.js 26.13.0 (newest stable; LTS 26.13.0 since 2026-10-28, Node 24 in maintenance; checked 2026-11-02) ships natively what older code pulled from npm:
native TypeScript type stripping (node file.ts), native .env
loading (node --env-file), built-in SQLite (node:sqlite), global WebSocket,
Promise.withResolvers(), global fetch, AbortSignal.timeout, node:test, Web Streams,
and node --run. Prefer the platform when its semantics fit; do not remove needed capabilities merely to reduce dependency count.
Rules
- Pre-flight: Inspect repo runtime & live Node.js LTS schedule first. Check
package.json
"engines", .nvmrc, .node-version, and check node -v to determine the active engine.
Check the official Node.js release schedule online to ensure target runtime aligns with
active LTS or the requested latest stable channel; verify support status from the current release schedule.
- Match the module system. In an ESM package (
"type": "module"or.mjs)
write ES import statements, with explicit file extensions on relative imports when the
project compiles with NodeNext. Don't convert a CommonJS package to ESM as a
side effect of another change.
- Import built-ins with the
node:prefix (node:fs/promises,node:path,node:sqlite). - Prefer native platform features over npm bloat:
- Use native
node --env-file=.envinstead of installingdotenv. - Use native
globalThis.fetch()instead ofaxiosornode-fetch. - Native WebSocket covers standard clients; verify the required client/server features before replacing ws.
- Use native
node:sqlitefor local structured caching and data storage. - Use native
node:cryptorandomUUID()instead ofuuid. - Use
Promise.withResolvers()instead of hand-rolled deferred promises.
- Give every operation that can wait a deadline: pass an
AbortSignalto
fetch calls, child processes, and timers; combine user cancellation with a
timeout using AbortSignal.any.
- Handle
ENOENTby reading inside try/catch and branching onerr.code;
checking access first is a race (TOCTOU).
- Never block the event loop in a server or CLI hot path: no
*Syncfs calls
or CPU-heavy loops on request paths.
- Pass arguments to child processes as an array (
execFile/spawn), never by
interpolating into a shell string.
- Every promise is awaited, returned, or explicitly handled; let entry points
report unhandled rejections instead of swallowing them.
Patterns
import { execFile } from 'node:child_process';
import { randomBytes } from 'node:crypto';
import { readFile, rename, writeFile } from 'node:fs/promises';
import { setTimeout as delay } from 'node:timers/promises';
import { promisify } from 'node:util';
const execFileAsync = promisify(execFile);
// Deadline plus user cancellation.
export async function getJson(url: string, signal?: AbortSignal): Promise<unknown> {
const deadline = AbortSignal.timeout(10_000);
const res = await fetch(url, { signal: signal ? AbortSignal.any([signal, deadline]) : deadline });
if (!res.ok) throw new Error(`GET ${url} failed with ${res.status}`);
return res.json();
}
// Missing file is an expected outcome, not an exception.
export async function readOptional(path: string): Promise<string | undefined> {
try {
return await readFile(path, 'utf8');
} catch (err) {
if ((err as NodeJS.ErrnoException).code === 'ENOENT') return undefined;
throw err;
}
}
// Atomic replace: readers never observe a half-written file.
export async function writeAtomic(target: string, data: string): Promise<void> {
const tmp = `${target}.${randomBytes(4).toString('hex')}.tmp`;
await writeFile(tmp, data);
await rename(tmp, target);
}
// Cancellable delay — the promise API takes a signal; the global setTimeout does not.
await delay(500, undefined, { signal: AbortSignal.timeout(5_000) });
// Arguments as an array — no shell, no injection.
const { stdout } = await execFileAsync('git', ['log', '--oneline', '-5'], {
signal: AbortSignal.timeout(15_000),
});
// Partial failure is acceptable: collect every outcome.
const results = await Promise.allSettled(urls.map((url) => getJson(url)));
const failed = results.filter((r) => r.status === 'rejected');
In ESM, __dirname doesn't exist: use import.meta.dirname on current Node, or
path.dirname(fileURLToPath(import.meta.url)) on older releases.
Anti-patterns
| Anti-pattern | Why it hurts | Instead |
|---|---|---|
| fetch(url) with no signal | Hangs forever on a stalled server | AbortSignal.timeout() |
| exec(\cmd ${input}\) | Shell injection | execFile with an argument array |
| existsSync then readFile | Race between check and use | try/catch on the read |
| readFileSync in a request handler | Blocks every other request | node:fs/promises |
| Catching an AbortError and continuing silently | Hides timeouts and cancellations | Rethrow, or report it as a timeout |
| writeFile directly over a config or state file | A crash leaves it truncated | Write to a temp file, then rename |
Before returning
- [ ] Module system and Node version match what the project declares
- [ ] Built-ins imported with
node:; no new dependency the platform covers - [ ] Every network call, child process, and delay is cancellable or bounded
- [ ] Child-process arguments passed as arrays
- [ ] Missing-file and abort cases handled deliberately; no floating promises
Current runtime and ownership
Latest stable checked 2026-11-02: Node.js 26.13.0 (LTS 26.13.0; Node 24 in maintenance), Bun 1.4.4,
pnpm 12.16.1. Refresh the official release/registry before recommendations.
nodejs.org/dist/index.json also lists alpha and Current builds; name the channel you checked.
Native TypeScript execution strips supported syntax; it does not typecheck or
implement tsconfig path aliases. Verify the real module/resolution contract.
Native WebSocket is a client API; server behavior may require a dependency.
For atomic replacement, use an exclusively created temp file in the same
directory and clean only owned intermediates on failure. Rename is not durability,
cross-filesystem atomicity or concurrency control. Recheck generation after awaits.
Skills in scope
typescript-strict— for typing Node.js APIs and boundariessecurity-scanner— for shell, path, and SSRF exposure in I/O codetesting— for testing async and time-based logic with fake timers