What is the fs module in Node.js?
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.