Study notes · 10% of the exam

File System, Processes and Signals

Know how Node.js talks to the file system and the OS: which fs calls block, how paths resolve, how processes start, exit and receive signals, and how to run child processes and shut down safely in containers.

Key points

  1. 1

    Sync fs calls block the whole event loop. Use them at startup or in CLIs, and use fs/promises or streams inside servers; stream large files instead of readFile.

  2. 2

    Relative fs paths resolve against process.cwd(), not the module. Build paths from __dirname (CommonJS) or import.meta.dirname (ESM, Node.js 20.11+). path.join concatenates; path.resolve restarts at the last absolute segment.

  3. 3

    Avoid check-then-act races: open with the wx flag instead of existsSync + write, and write atomically with a temp file in the same folder plus rename (renaming across filesystems fails with EXDEV).

  4. 4

    Prefer process.exitCode over process.exit(): exit() abandons timers and pending stdout writes. The exit event only runs synchronous code; beforeExit can schedule more work.

  5. 5

    Signals: SIGINT and SIGTERM exit with 128 + signal (130, 143) unless you add a listener, which removes the default. As PID 1 in a container, Node.js ignores SIGTERM unless it handles it; use the exec form of CMD and --init.

  6. 6

    child_process: exec uses a shell and buffers up to maxBuffer (1 MiB); execFile and spawn take argument arrays without a shell; fork adds an IPC channel. Consume piped stdout, and use detached + unref() for background children.

  7. 7

    Built-ins replace common packages: --env-file (environment values win, later files override), util.parseArgs (strict by default), --watch, node:sqlite, and the opt-in --permission model.

Common traps

  • target.startsWith(base) is not a path traversal check: /srv/uploads-old passes for base /srv/uploads. Use path.relative and reject results starting with ...

  • child.kill() signals only the direct child. Servers started through npm or a shell survive as orphans and keep their ports.

  • fs.watch on a single file stops seeing changes after an editor's atomic save, because it watches the old inode. Watch the directory instead.

Test yourself on File System, Processes and Signals

Ten questions, with the answer and explanation after each one.