Node.js Core Modules Cheat Sheet

Quick reference guide for Node.js core modules: fs/promises, path, stream, http, events, child_process, and Buffer.

Languages
nodejs
backend
javascript

Node.js is an open-source, cross-platform JavaScript runtime environment. Its built-in standard modules allow server-side I/O, file handling, HTTP networking, and process management.

File System (`fs/promises`)

The fs/promises module provides asynchronous file operations returning Promises.

typescript
import fs from "node:fs/promises";
import path from "node:path";

// Read and parse JSON file
async function loadConfig(filePath: string) {
  const absolutePath = path.resolve(filePath);
  const rawData = await fs.readFile(absolutePath, "utf-8");
  return JSON.parse(rawData);
}

// Write content safely
async function saveLog(filePath: string, message: string) {
  await fs.mkdir(path.dirname(filePath), { recursive: true });
  await fs.appendFile(filePath, `${new Date().toISOString()} - ${message}\n`);
}

Path Operations (`node:path`)

Table
MethodOutput ExampleDescription
path.join('/a', 'b', 'c.txt')/a/b/c.txtJoin path segments with OS separator
path.resolve('dir', 'file.js')/abs/cwd/dir/file.jsResolve to absolute path from CWD
path.extname('app.config.json').jsonExtract extension including dot
path.basename('/a/b/file.txt')file.txtExtract filename portion
path.dirname('/a/b/file.txt')/a/bExtract directory path portion

HTTP Server (`node:http`)

Create a lightweight native HTTP server without third-party dependencies.

typescript
import http from "node:http";

const server = http.createServer((req, res) => {
  if (req.url === "/api/health" && req.method === "GET") {
    res.writeHead(200, { "Content-Type": "application/json" });
    res.end(JSON.stringify({ status: "ok", timestamp: Date.now() }));
    return;
  }

  res.writeHead(404, { "Content-Type": "text/plain" });
  res.end("Not Found");
});

server.listen(3000, () => {
  console.log("Server listening on http://localhost:3000");
});

EventEmitter (`node:events`)

typescript
import { EventEmitter } from "node:events";

class UserNotifier extends EventEmitter {}

const notifier = new UserNotifier();

// Subscribe to event
notifier.on("userJoined", (user: { name: string }) => {
  console.log(`User registered: ${user.name}`);
});

// Emit event
notifier.emit("userJoined", { name: "Alice" });

Buffer & Binary Data (`node:buffer`)

Table
OperationCode SnippetPurpose
Create from StringBuffer.from("Hello", "utf-8")Convert text to raw byte buffer
Create from Base64Buffer.from("aGVsbG8=", "base64")Decode base64 binary buffer
Output Base64buf.toString("base64")Encode buffer into base64 string
Buffer Lengthbuf.lengthByte count of the buffer

Common Pitfalls & Tips

[!WARNING] Synchronous methods like fs.readFileSync() block the entire single-threaded Event Loop! Always prefer fs/promises or callback equivalents in server environments.

[!TIP] Use the explicit node: prefix when importing core modules (e.g., import fs from 'node:fs/promises') to prevent conflicts with npm packages and improve load performance.