Filesystem and Path
Everyday filesystem and path utilities for scripts and servers. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Search across all documentation pages
Everyday filesystem and path utilities for scripts and servers. Results appear in the same fence: same-line // comments when short, multiline // blocks below the sample when not.
Promise-based whole-file IO for modest sizes.
import { readFile, writeFile, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const dir = await mkdtemp(path.join(tmpdir(), "n-"));
const f = path.join(dir, "notes.txt");
await writeFile(f, "hi", "utf8");
await readFile(f, "utf8") // "hi"Stream large files instead of buffering entire contents.
import { createReadStream } from "node:fs";
import { Readable } from "node:stream";
// createReadStream("big.log") for real files
const parts: string[] = [];
for await (const c of Readable.from(["a", "b"])) parts.push(String(c));
parts // ["a", "b"]Create nested directories in one call.
import { mkdir, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const base = await mkdtemp(path.join(tmpdir(), "m-"));
const nested = path.join(base, "data", "cache");
await mkdir(nested, { recursive: true });
// nested now existsjoin concatenates segments; resolve makes an absolute path from cwd.
import path from "node:path";
path.join("data", "a.json") // "data/a.json"
path.isAbsolute(path.resolve("data", "a.json")) // trueConvert import.meta.url or file URLs to filesystem paths.
import { fileURLToPath } from "node:url";
const file = fileURLToPath(import.meta.url);
file.endsWith(".ts") || file.endsWith(".js") // trueList directory entries with file type info without extra stats when supported.
import { readdir, mkdtemp, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const dir = await mkdtemp(path.join(tmpdir(), "r-"));
await writeFile(path.join(dir, "a.txt"), "x");
const entries = await readdir(dir, { withFileTypes: true });
entries.map((e) => e.name) // ["a.txt"]
entries[0].isFile() // truePrefer try/catch on open/stat over a separate exists race.
import { stat } from "node:fs/promises";
try {
await stat("/no/such/file-xyz");
} catch (e: any) {
e.code // "ENOENT"
}Remove files or trees with rm.
import { rm, mkdir, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const dir = await mkdtemp(path.join(tmpdir(), "rm-"));
await mkdir(path.join(dir, "sub"));
await rm(dir, { recursive: true, force: true });
// dir removed; no throwCopy or rename/move within a filesystem.
import { copyFile, rename, writeFile, readFile, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const dir = await mkdtemp(path.join(tmpdir(), "c-"));
const a = path.join(dir, "a.txt");
const b = path.join(dir, "b.txt");
await writeFile(a, "1");
await copyFile(a, b);
await readFile(b, "utf8") // "1"Create a unique temporary directory safely.
import { mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const dir = await mkdtemp(path.join(tmpdir(), "job-"));
dir.includes("job-") // trueAppend lines to a log file without reading the whole file.
import { appendFile, readFile, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const f = path.join(await mkdtemp(path.join(tmpdir(), "l-")), "app.log");
await appendFile(f, "ok\n");
await readFile(f, "utf8") // "ok\n"Split and rebuild paths with parse / format.
import path from "node:path";
const p = path.parse("/tmp/a/b.txt");
p.base // "b.txt"
p.name // "b"
p.ext // ".txt"
path.format({ dir: p.dir, name: p.name, ext: ".md" })
// "/tmp/a/b.md"Low-level handle for positioned reads/writes.
import { open, writeFile, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const f = path.join(await mkdtemp(path.join(tmpdir(), "h-")), "d.bin");
await writeFile(f, Buffer.from("abcd"));
const fh = await open(f, "r");
const { buffer, bytesRead } = await fh.read(Buffer.alloc(2), 0, 2, 0);
await fh.close();
bytesRead // 2
buffer.toString() // "ab"Watch for changes - prefer chokidar for production cross-platform robustness.
import { watch } from "node:fs";
// const w = watch("config.json", () => reload());
// w.close() on shutdown
typeof watch // "function"Check permissions with access and mode constants.
import { access, constants, writeFile, mkdtemp } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
const f = path.join(await mkdtemp(path.join(tmpdir(), "a-")), "p.txt");
await writeFile(f, "x");
await access(f, constants.R_OK);
// resolves if readable; throws if notStack versions: Node.js 24.18.0 (LTS line 24) · TypeScript 5.6+ · npm 10+
Reviewed by Chris St. John·Last updated Jul 19, 2026