Back to case studies
Node.jsFundamental

Reading and writing files with the fs module

Scenario

Context A script must read a JSON configuration file, modify one field, and write the updated content back to disk. The Problem The developer is using synchronous blocking calls (fs.readFileSync) inside an async context, causing event loop blocking. Current Code const data = fs.readFileSync("config.json"); const config = JSON.parse(data); config.version = "2.0"; fs.writeFileSync("config.json", JSON.stringify(config)); Constraints Replace synchronous calls with fs.promises equivalents. Use async/await for readability. Handle file-not-found errors explicitly. Expected Deliverable A Node.js script using fs.promises to read, modify, and write a JSON file asynchronously with proper error handling.

Targeted skills

nodejsfsasyncfile-iopromises
    Reading and writing files with the fs module — Node.js hands-on scenario | CodeDuel