🌍 NodeJS · flashcards

NodeJS File System Flashcards

50 question-and-answer cards covering File System as it is examined in NodeJS. 24 of them are printed below, taken from across the deck — no signup, no paywall on the preview.

50Cards in deck
24Free preview
5Syllabus topics
~150Chars per answer
FreePrice

24 sample cards from the File System deck

Sampled from the end of the deck, so these are different cards from the ones shown on the syllabus page.

  1. How do you create a Buffer from an existing string?

    `Buffer.from('hello', 'utf8')`. You can also create one from an array of bytes: `Buffer.from([0x68, 0x69])`.

  2. How is the byte length of a Buffer determined, and does it always equal string length?

    Via the `buf.length` property (bytes allocated). It does not always equal the string's character count because multibyte UTF-8 characters occupy more than one byte, e.g. 'é' is $2$ bytes.

  3. Why is the `Buffer` constructor `new Buffer()` deprecated?

    It was ambiguous and unsafe: its behavior depended on argument type, and numeric arguments produced uninitialized memory. It's replaced by `Buffer.alloc()`, `Buffer.allocUnsafe()`, and `Buffer.from()`.

  4. How do you convert a Buffer back into a string with a chosen encoding?

    `buf.toString('utf8')` (or `'hex'`, `'base64'`, `'ascii'`, `'latin1'`). Optional start/end arguments select a byte range.

  5. Which encodings are commonly supported by Node.js Buffers?

    `'utf8'`, `'utf16le'` (`'ucs2'`), `'latin1'` (`'binary'`), `'ascii'`, `'base64'`, `'base64url'`, and `'hex'`.

  6. Are Buffers resizable after creation?

    No. A Buffer has a fixed length set at allocation. To "grow" it you must allocate a new, larger Buffer and copy the data (e.g. via `Buffer.concat()`).

  7. How do you concatenate multiple Buffers into one?

    `Buffer.concat([buf1, buf2, ...])`, optionally passing a total length as the second argument for efficiency.

  8. How do you create a new directory synchronously?

    `fs.mkdirSync('newdir')`. Asynchronously: `fs.mkdir('newdir', callback)` or `await fs.promises.mkdir('newdir')`.

  9. How do you create nested directories (parents that don't yet exist) in one call?

    Pass the `recursive: true` option: `fs.mkdirSync('a/b/c', { recursive: true });`. Without it, creating a directory whose parent is missing throws `ENOENT`.

  10. What error occurs if you call `fs.mkdir` on a directory that already exists (without recursive)?

    An `EEXIST` error. With `{ recursive: true }`, an existing target does not throw.

  11. How do you delete an empty directory?

    `fs.rmdirSync('dir')` or async `fs.rmdir('dir', callback)`. It fails with `ENOTEMPTY` if the directory contains files.

  12. How do you delete a directory and all of its contents recursively (modern API)?

    `fs.rmSync('dir', { recursive: true, force: true });` (or the async `fs.rm`). `force: true` ignores errors if the path doesn't exist. `fs.rmdir` with `recursive` is deprecated in favor of `fs.rm`.

  13. What does the `force: true` option do in `fs.rm()`?

    It suppresses errors when the target path does not exist (no `ENOENT` thrown) and, combined with `recursive`, allows removing non-empty directories quietly.

  14. How do you delete a single file (not a directory) in Node.js?

    `fs.unlinkSync('file.txt')` or async `fs.unlink('file.txt', callback)`. `unlink` is for files; `rmdir`/`rm` handle directories.

  15. How do you create a temporary directory with a unique name?

    `fs.mkdtempSync(prefix)`, e.g. `fs.mkdtempSync(path.join(os.tmpdir(), 'app-'))`, which appends six random characters and returns the created path.

  16. How do you read the contents (entries) of a directory synchronously?

    `const entries = fs.readdirSync('dir');` returns an array of the file and subdirectory names (strings) contained in `dir`.

  17. How do you read directory contents asynchronously with Promises?

    `const entries = await fs.promises.readdir('dir');` inside an `async` function, returning an array of entry names.

  18. What does the `{ withFileTypes: true }` option do for `fs.readdir`?

    It returns an array of `fs.Dirent` objects instead of plain name strings, letting you call `dirent.isDirectory()` or `dirent.isFile()` without a separate `stat` call.

  19. How can you list a directory tree recursively with a single `fs.readdir` call?

    Pass `{ recursive: true }` (Node 18.17+): `fs.readdirSync('dir', { recursive: true })` returns all nested entries' relative paths.

  20. How do you distinguish files from subdirectories when listing a directory?

    Use `fs.statSync(path)` (or `lstatSync`) and check `stats.isFile()` / `stats.isDirectory()`, or use `readdir` with `{ withFileTypes: true }` and check each `Dirent`'s `isFile()`/`isDirectory()`.

  21. What is the difference between `fs.stat` and `fs.lstat`?

    `fs.stat` follows symbolic links and reports info about the target file. `fs.lstat` reports info about the symlink itself without following it.

  22. Which module should you use to build cross-platform file paths when reading directories, and why?

    The `path` module (e.g. `path.join(dir, name)`), because it uses the correct OS-specific separator (`/` vs `\`) and normalizes the result, avoiding manual string concatenation bugs.

  23. What does `fs.existsSync(path)` return, and why is `fs.exists` (callback) deprecated?

    `fs.existsSync` returns a boolean indicating whether the path exists. The callback `fs.exists` is deprecated because its callback signature (single boolean, no error) is inconsistent with Node's error-first convention and invites race conditions.

  24. What comparison summarizes callback-based vs Promise-based async `fs` APIs?

    Both are non-blocking. Callback API (`require('fs')`) uses error-first callbacks and can lead to nested "callback hell." Promise API (`require('fs/promises')`) supports `async/await`, cleaner chaining, and `try/catch` error handling.

What this deck covers

The File System deck follows the NodeJS File System syllabus — 2 chapters and 5 topics — so questions land on material that is genuinely examinable rather than trivia around it. That works out to roughly 25.0 cards per chapter.

Answers are written to be recallable, not just readable — averaging about 150 characters, which is long enough to carry the reasoning and short enough to say out loud.

A deck like this earns its keep on the second and third pass. Read the syllabus first so you know the shape of the subject, then use the cards to find the specific facts that have not stuck.

File System flashcards FAQ

How many File System flashcards are in this NodeJS deck?

50 cards. This page previews 24 of them, sampled evenly across the deck so you can judge the difficulty before installing anything.

Are these NodeJS flashcards free?

Yes. The preview here is free to read with no signup, and the full 50-card deck is free inside the Examius app.

What do the File System cards cover?

They follow the NodeJS File System syllabus — 2 chapters and 5 topics — so the questions track what is actually examinable.

How should I use these flashcards?

Read the syllabus first so you know the shape of the subject, then drill the deck. Examius schedules each card with spaced repetition, so cards you keep missing come back sooner and ones you know drift further apart.