AceDevHub
Intermediate Node.js Interview QuestionsIntermediateConcept

Node.js · Question 65

What is the difference between async and sync file system APIs in Node.js?

Direct answer

Sync fs methods (*Sync) block the event loop until libuv completes disk I/O — OK for CLI startup scripts; async fs.promises or callbacks yield the loop — required on servers handling concurrent requests.

File I/O uses the libuv thread pool (Q8) even for async APIs — but sync variants wait on the main thread, freezing timers, HTTP responses, and WebSocket pings.

Why interviewers ask

APIBlocks event loop?Typical use
fs.readFileSyncYesCLI config at boot, build scripts
fs.promises.readFileNo (main thread)API handlers, import scripts
fs.readFile + callbackNoLegacy codebases
fs.createReadStreamNo (chunked)Large files, content import
  • Server rule — never readFileSync per request on Fastify API.
  • Import scripts — AceDevHub content import runs async fs + JSON parse in tsx scripts.
  • Watch mode — fs.watch for dev reload; production prefers explicit deploy artifacts.
fs-async-sync.mjs
// CLI startup — sync acceptable
import fs from "node:fs";
const pkg = JSON.parse(fs.readFileSync("package.json", "utf8"));

// Server handler — async only
import fsp from "node:fs/promises";

export async function loadFixture(name) {
  const raw = await fsp.readFile(`fixtures/${name}.json`, "utf8");
  return JSON.parse(raw);
}