Skip to main content
NODEJS-BASICS5 MIN READ

Read JSON from disk without hiding errors

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

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.

Already have an account? Sign in

← Back to library
Contact us