Built-in APIs Basics
8 examples to get you started with Node built-in APIs - 6 basic and 2 intermediate.
Prerequisites
- Node.js 24.18.0 - built-ins ship with the runtime.
- Prefer
import from 'node:…'in ESM projects.
Basic Examples
1. node: Prefix Import
Import built-ins explicitly and avoid shadowing.
import { readFile } from 'node:fs/promises';
import { createServer } from 'node:http';
import path from 'node:path';node:prefix is optional but recommended in application code.- Built-ins are not in
package.jsondependencies. - Typings come from
@types/nodein devDependencies.
2. fs/promises readFile
Async file read without util.promisify.
import { readFile } from 'node:fs/promises';
const pkg = JSON.parse(await readFile('package.json', 'utf8'));
console.log(pkg.name);- Always specify encoding for text or get Buffer.
- Use
createReadStreamfor large files. - Handle
ENOENTwith try/catch for optional config files.
Related: fs and fs/promises - streams and flags
3. path.join and fileURLToPath
Portable paths in ESM without __dirname.
import path from 'node:path';
import { fileURLToPath } from 'node:url';
const __filename = fileURLToPath(import.meta.url);
const configPath = path.join(path.dirname(__filename), 'config.json');path.joinnormalizes separators for the platform.- Never concatenate paths with
/manually in library code. - Use
path.posix.joinfor object storage keys on all platforms.
Related: path and url - URL vs path
4. node:http Server
Minimal HTTP without Express.
import { createServer } from 'node:http';
createServer((req, res) => {
res.writeHead(200, { 'content-type': 'text/plain' });
res.end('ok');
}).listen(3000);- Fine for health checks and internal probes.
- Production APIs usually add Express 5 or Fastify 5 for routing.
- Set timeouts on server (
server.requestTimeout) in Node 24.
5. crypto.randomUUID
Identifiers without uuid package.
import { randomUUID } from 'node:crypto';
const id = randomUUID();
console.log(id);- Cryptographically strong UUID v4.
- Also
randomBytesfor tokens - encode as base64url. - See crypto for hashing and HMAC.
6. timers/promises sleep
Promise-based delay without npm.
import { setTimeout } from 'node:timers/promises';
await setTimeout(100);
console.log('after delay');- Accepts
AbortSignalfor cancellable delays. setIntervalstill callback-based for periodic work.- See Timers and Scheduler.
Intermediate Examples
7. fetch (built-in)
HTTP client without axios for simple outbound calls.
const res = await fetch('https://nodejs.org/dist/index.json');
if (!res.ok) throw new Error(`HTTP ${res.status}`);
const data = await res.json();- Global
fetchin Node 24 - same API as browsers. - Configure agents/timeouts via undici options when needed.
- For advanced retry policies see got/axios.
8. util.parseEnv and styleText (Node 24)
Modern utilities for env and CLI output.
import { parseEnv } from 'node:util';
import { styleText } from 'node:util';
const env = parseEnv('PORT=3000\nNODE_ENV=production');
console.log(styleText('green', 'boot'), env.PORT);parseEnvparses dotenv-style strings in memory.- Prefer Zod for production env validation.
- See util and diagnostics.
Related: Zod at Boundaries - validate env
Stack versions: This page was written for Node.js 24.18.0 (Active LTS), npm 10+, TypeScript 5.6+, Express 5, Fastify 5, and NestJS 11.