10 / 16

How can you recursively list all files and directories in a given directory using Node.js?

Difficulty: 5/10
file system (fs), asynchronous control flow, streams & generators

You can recursively list files and directories in a given directory using a recursive function that traverses the directory structure. Here's a simplified example:

javascript

Scenario Questions

0-2 years experience

  1. 1We need to build a local CLI tool that scans a project folder and lists all `.js` files. How would you write a function in Node.js to recursively walk through the directories and collect these file paths? Would you use synchronous or asynchronous FS methods here, and why?
  2. 2Imagine you wrote a recursive directory walker using `fs.readdirSync`, but when running it on a large folder, the CLI completely freezes and doesn't respond to user input. What's happening under the hood, and how would you refactor it to keep the process responsive?

2-5 years experience

  1. 1We are building a local file-watcher feature for a development tool. When scanning a user's workspace, some folders might be restricted or locked by the OS, causing the entire scan to crash. How would you structure your recursive directory traversal to gracefully skip unreadable directories while still collecting the rest of the files?
  2. 2You've inherited a legacy Node.js script that uses recursive `fs.stat` calls inside a loop to check if an item is a file or directory. It's running incredibly slowly on directories with thousands of files. How can we optimize this using modern `fs` API options to avoid making those extra stat calls?

5-8 years experience

  1. 1We need to index a massive shared network drive containing millions of files. A standard recursive `Promise.all` approach will quickly exhaust the Node.js heap memory or hit call stack limits. How would you design a memory-efficient directory crawler using streams or async generators to process files lazily?
  2. 2Our build tool crashes with a 'Maximum call stack size exceeded' or hangs indefinitely when users have complex monorepos with symlinks (like pnpm stores). How would you refactor our recursive directory traversal algorithm to safely handle circular symlinks and extremely deep directory structures?

8+ years experience

  1. 1We are designing a cross-platform desktop application that needs to index the user's entire hard drive for a global search feature. Given the differences in file systems (NTFS, APFS, ext4), I/O bottlenecks, and OS-level permission models, how would you architect this indexing service to minimize CPU/disk impact and ensure the UI thread remains completely fluid?
  2. 2Our enterprise asset pipeline needs to scan and process millions of media assets across distributed storage mounts. Instead of writing a custom Node.js recursive walker, what are the architectural trade-offs of offloading this to native OS binaries (like `find` or `fd`) via child processes versus using a pure Node.js worker-thread pool?

Follow-up Questions

  • How would your implementation handle a directory structure that contains circular symbolic links?
  • If a directory contains 10 million files, how do we prevent our Node process from running out of memory during the scan?
  • What happens if one subdirectory in the middle of the tree throws a permission denied error, and how do we prevent it from crashing the entire traversal?
Share

Share via WhatsApp, X, Facebook, LinkedIn or copy link. Open Graph preview enabled.