Chapter 24: Node.js File System Module

1. What is the File System module?

node:fs is the built-in module that lets your Node.js program read files, write files, create/delete directories, check file existence, get file stats, watch for changes, and more.

It is one of the most frequently used core modules in backend development.

Important facts (2025–2026):

  • There are three main APIs:

    Style Syntax example When to use it today Recommendation
    Callback fs.readFile(…, callback) Very old code only Avoid
    Synchronous fs.readFileSync(…) CLI tools, scripts, startup config Use carefully
    Promise-based import fs from ‘node:fs/promises’ Almost all modern code Best choice
  • Always use the node: prefix in imports → import fs from ‘node:fs/promises’ (modern & safe style)

  • Never block the event loop with heavy synchronous file operations in a server

2. Modern recommended way – Promise-based API

This is how most professional code is written in 2025–2026.

JavaScript

Run:

Bash

3. Very common real-world patterns

Pattern 1 – Safe JSON config loading (very frequent)

JavaScript

Pattern 2 – Writing structured logs

JavaScript

Pattern 3 – Copy file with progress feedback

JavaScript

Pattern 4 – Read all JSON files in a folder

JavaScript

4. Synchronous API – when & how to use it safely

Only use synchronous methods in:

  • CLI tools
  • Build scripts
  • Startup / configuration phase (before server starts)
  • Tests

Never use readFileSync / writeFileSync inside request handlers!

JavaScript

5. Very important error codes you should know

Error code Meaning Common action
ENOENT No such file or directory File not found → use defaults / 404
EEXIST File / directory already exists Skip creation or overwrite if safe
EACCES Permission denied Log error, tell user to check permissions
EISDIR Is a directory You tried to read a folder as a file
ENOTDIR Not a directory You tried to treat a file as a folder

Best practice – handle common cases

JavaScript

6. Watching for file changes (very useful for dev tools)

JavaScript

More powerful watcher (2026 style)

JavaScript

Summary – Decision guide 2025–2026

Situation Recommended API Why?
Modern server / API code node:fs/promises + async/await Clean, non-blocking, error handling easy
CLI tools / scripts / build steps fs.readFileSync / fs.writeFileSync Simpler, no async boilerplate
Startup config loading Either (sync is acceptable here) Runs once before server starts
Watching files (hot reload, config) fs.watch Built-in, lightweight
Very large files (> few hundred MB) Streams (createReadStream) Memory efficient

Which part of the File System module would you like to explore much deeper next?

  • Streams – reading/writing large files efficiently
  • File watching in real applications (config reload, file upload watchers)
  • Atomic file operations (safe write + rename pattern)
  • Temporary files & cleanup
  • Complete example – config loader + logger + migration runner

Just tell me which direction feels most useful — I’ll continue with detailed, production-ready examples. 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *