After this lesson, you will be able to: Use the core modules: fs, path, os, http, events, stream, buffer, crypto.
Node ships everything you need for most server tasks without npm install.
The daily-driver stdlib.
import { readFile, writeFile, mkdir, readdir } from "node:fs/promises";import path from "node:path";import os from "node:os";// fs/promises (async; use this; not fs)const data = await readFile("data.json", "utf-8");await writeFile("out.txt", "hello");await mkdir("./tmp", { recursive: true });// pathconst joined = path.join(__dirname, "src", "file.ts");const ext = path.extname("/a/b/file.ts"); // ".ts"// osconsole.log(os.platform(), os.tmpdir(), os.cpus().length, os.homedir());
Used everywhere in Node (streams, http, process).
import { EventEmitter } from "node:events";const bus = new EventEmitter();bus.on("user-created", (user) => console.log("new user:", user.id));bus.emit("user-created", { id: "u1" });// once = handler runs once + auto-removesbus.once("shutdown", () => console.log("bye"));
Hashing + random + binary.
import { randomBytes, createHash, scrypt } from "node:crypto";import { promisify } from "node:util";// Random tokenconst token = randomBytes(32).toString("hex");// Hashconst sha = createHash("sha256").update("hello").digest("hex");// Password hashing (use scrypt or argon2 via npm, never SHA-256)const scryptAsync = promisify(scrypt);const salt = randomBytes(16);const hash = await scryptAsync("password", salt, 64) as Buffer;// Buffer (binary data)const b = Buffer.from("hello", "utf-8");console.log(b.toString("hex")); // 68656c6c6f
Using callback `fs.readFile(path, cb)` in new code. fs/promises is cleaner. Mixing path separators manually. Use path.join. Storing passwords as SHA-256. Use bcrypt / scrypt / argon2 with salt + work factor. Forgetting `node:` prefix on imports. Best practice in modern Node.
Pick the security answer.
Sign in and purchase access to unlock this lesson.