🌍 NodeJS · subject

NodeJS File System Syllabus

Every chapter and topic of File System examined in NodeJS — 2 chapters, 5 topics, plus 50 flashcards written against it.

2Chapters
5Topics
0Sub-topics
~4hEst. first pass
7%Of NodeJS
50Flashcards

File System syllabus — full chapter and topic list

Expand any chapter to see its topics and sub-topics. This is the whole examinable outline for File System in NodeJS, not a summary of it.

  1. Reading and Writing Files

    3 topics
    • Synchronous vs Asynchronous
    • Streams
    • Buffers
  2. Directory Operations

    2 topics
    • Creating and Deleting Directories
    • Reading Directory Contents

File System flashcards for NodeJS

21 of 50 cards from the File System deck — real questions with worked answers.

  1. In Node.js, which core module provides the API for interacting with the file system?

    The `fs` module, imported with `const fs = require('fs')` (or `import fs from 'fs'`). It offers synchronous, callback-based asynchronous, and Promise-based APIs.

  2. What is the fundamental difference between synchronous and asynchronous file system methods in Node.js?

    Synchronous methods block the event loop until the operation completes and return the result directly (or throw). Asynchronous methods return immediately and deliver the result later via a callback, Promise, or event, keeping the event loop free.

  3. How are synchronous `fs` methods named by convention, and give an example?

    They end with the `Sync` suffix, e.g. `fs.readFileSync()`, `fs.writeFileSync()`, `fs.mkdirSync()`. Non-suffixed names like `fs.readFile()` are asynchronous.

  4. How do you read a file synchronously and get its contents as a UTF-8 string?

    `const data = fs.readFileSync('file.txt', 'utf8');` The encoding argument makes it return a string; without it, a `Buffer` is returned.

  5. How do you read a file asynchronously using the classic callback API?

    `fs.readFile('file.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data); });` The error-first callback receives `err` as its first argument.

  6. What is the "error-first callback" convention in Node.js asynchronous APIs?

    The callback's first parameter is reserved for an error object (`null` if none), and subsequent parameters hold the successful result, e.g. `(err, data) => {}`.

  7. How do you use the Promise-based `fs` API to read a file asynchronously?

    Import `const fs = require('fs/promises');` (or `fs.promises`), then `const data = await fs.readFile('file.txt', 'utf8');` inside an `async` function.

  8. Why are synchronous `fs` calls generally discouraged in server code?

    They block the single-threaded event loop, so no other requests or timers can be processed until the operation finishes, harming throughput and responsiveness under load.

  9. When is it acceptable to use synchronous `fs` methods?

    During startup/initialization (e.g. loading config), in CLI scripts, or one-off tooling where blocking is harmless and simpler code is preferable to concurrency.

  10. How do you catch errors from synchronous vs asynchronous `fs` operations?

    Synchronous: wrap in `try/catch` since they throw. Callback async: check the `err` argument. Promise async: use `.catch()` or `try/catch` with `await`.

  11. What does `fs.writeFileSync('out.txt', 'hello')` do if the file already exists?

    It overwrites (truncates and replaces) the file's contents with the new data. To append instead, use `fs.appendFileSync()` or the `{ flag: 'a' }` option.

  12. What is a Stream in Node.js?

    An abstract interface for working with streaming data, processing it in small chunks over time rather than loading the whole payload into memory at once. Streams are instances of `EventEmitter`.

  13. What are the four fundamental types of streams in Node.js?

    Readable (data can be read from it), Writable (data can be written to it), Duplex (both readable and writable), and Transform (a duplex stream that modifies data as it passes through).

  14. Give a real example of each stream type: Readable, Writable, Duplex, Transform.

    Readable: `fs.createReadStream()`. Writable: `fs.createWriteStream()`. Duplex: a TCP socket (`net.Socket`). Transform: `zlib.createGzip()` compression.

  15. How do you create a readable stream from a file?

    `const rs = fs.createReadStream('file.txt', { encoding: 'utf8', highWaterMark: 64 * 1024 });` then listen for `'data'`, `'end'`, and `'error'` events.

  16. What key events does a Readable stream emit?

    `'data'` (a chunk is available in flowing mode), `'end'` (no more data), `'error'` (a problem occurred), `'close'`, and `'readable'` (data is available to read in paused mode).

  17. What key events does a Writable stream emit?

    `'drain'` (the buffer emptied and it's safe to write again after backpressure), `'finish'` (all data flushed after `end()`), `'error'`, and `'close'`, plus `'pipe'`/`'unpipe'`.

  18. What is the purpose of `stream.pipe()`?

    It connects a Readable stream's output to a Writable stream's input, automatically forwarding data chunks and managing backpressure, e.g. `readStream.pipe(writeStream);`.

  19. What is backpressure in Node.js streams?

    A flow-control mechanism where a slow Writable consumer signals a fast Readable producer to pause. `write()` returning `false` indicates the internal buffer is full; the producer waits for the `'drain'` event before resuming.

  20. What does the `highWaterMark` option control on a stream?

    The buffer size threshold (in bytes for binary, or number of objects in object mode) that triggers backpressure. It defaults to 64 KiB ($64 \times 1024 = 65536$ bytes) for file streams and 16 KiB for others.

  21. Why is `stream.pipeline()` preferred over `pipe()` for chaining streams?

    `pipeline()` (from the `stream` module) properly propagates errors and automatically destroys/cleans up all streams on failure, whereas manual `pipe()` chains can leak resources and swallow errors.

See more File System flashcards →

Planning File System for NodeJS

File System is about 7% of the NodeJS syllabus by topic count — 5 of 72 topics, spread over 2 chapters. At roughly 45 minutes per topic plus 12 minutes per sub-topic, a first pass runs to about 4 hours.

Work top-down: read the chapter, then tick topics off individually rather than marking the whole chapter done. Sub-topics are where silent gaps hide.

File System (NodeJS) FAQ

What is in the NodeJS File System syllabus?

File System is split into 2 chapters — Reading and Writing Files and Directory Operations, containing 5 topics and 0 sub-topics in total.

How is File System structured in the NodeJS syllabus?

2 chapters. File System accounts for about 7% of the topics in the whole NodeJS syllabus (5 of 72).

How long should I spend on File System for NodeJS?

Budget around 4 hours for a first pass through File System — about 45 minutes per topic plus 12 minutes per sub-topic across its 5 topics. Add revision cycles on top.

Are there flashcards for NodeJS File System?

Yes — a 50-card File System deck. Sample cards are printed on this page, and the full deck is free in the Examius app with spaced repetition scheduling.