File metadata describes the path at the time it was inspected. A later read can still fail if the file changes.
Worked example
import * as fs from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
const dir = await fs.mkdtemp(join(tmpdir(), "helpers-"));
const file = join(dir, "note.txt");
await fs.writeFile(file, "build");
try {
console.log((await fs.stat(file)).size);
} finally {
await fs.rm(dir, { recursive: true, force: true });
}Result
5Implementation notes
A metadata check followed by a separate operation can race with a file move. Handle failure where the operation is actually attempted.
Try it yourself
Compare a directory entry and a regular file before attempting to read either.
Open the Node.js example →Editorial date June 26, 2025 · Published December 6, 2026