Skip to main content

Read JSON from disk without hiding errors

Build an async JSON file loader that separates read errors, parse errors, and shape validation.

nodejs-basics5 min read

Build loadSettings(path) for a Node.js CLI so it reads JSON asynchronously and reports the right kind of failure. Read -> parse -> validate -> return typed settings. The novice move is one broad catch that throws Cannot read config. That message hides whether the file is missing, the JSON is malformed, or the expected fields are absent. Step 1 Use await readFile(path, 'utf8') from node:fs/promises. Async file I/O keeps the function promise-based and avoids blocking a larger process. Step 2 Wrap JSON.parse(text) so syntax errors become settings.json is not valid JSON with the original error as cause. Parsing is a…

Read the full lesson

Sign up free: one personalized lesson every day, matched to your role and goals.

← Back to library

Contact us