What is the fs module in Node.js?
Why Interviewers Ask This
This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Node.js topics. It also reveals how well you can explain technical ideas to non-experts.
Answer
The fs (File System) module is a core Node.js module that provides an API for interacting with the file system. It supports both synchronous (blocking) and asynchronous (non-blocking) operations. Key methods: fs.readFile(path, encoding, callback) / fs.readFileSync() — read file contents; fs.writeFile(path, data, callback) / fs.writeFileSync() — write (or overwrite) a file; fs.appendFile() — append data; fs.unlink() — delete a file; fs.mkdir() / fs.mkdirSync() — create directory; fs.readdir() — list directory contents; fs.stat() — get file metadata; fs.rename() — rename/move a file; fs.watch() — watch for file changes. The fs/promises submodule (Node.js 10+) provides Promise-based versions: const fs = require("fs/promises");. Always prefer async methods in production to avoid blocking the event loop.
Common Mistake
Many candidates answer correctly but can't explain the 'why'. Always be prepared to justify your answer with a concrete example or use case from your Node.js experience.