Programming foundations

Stage 6 · Lesson 3 of 8

Files and JSON in Node.js

Build and ship your first projects

is native to JavaScript, so parsing JSON takes one call. JSON.parse turns text into objects and arrays, and JSON.stringify(data, null, 2) turns them back into readable text. For in , use the version built on : import { readFile, writeFile } from "node:fs/promises", then await readFile(path, "utf8").

Promises are values that arrive later, and await waits for one. A missing file rejects with an error whose code is "ENOENT". You can catch that to create a starter file on the first run.

JSON.parse throws a SyntaxError on invalid text. Validate what you load, because a missing property gives undefined rather than an error, and the bug shows up much later. Keep file code in its own module, so the core logic works on plain objects.

A question to check yourself, an AI practice session and 3 cheat sheet entries come when you add the roadmap to your account. Add it

Resources for this stage

Start with· follow one of these

  • GitHub repoFree
    Project Based Learning

    Community maintained · Beginner

    Follow one guided build in your language, then build your own without a guide

  • BookFree
    Eloquent JavaScript

    Marijn Haverbeke · Intermediate

    Free: the project chapters (a robot, a programming language, a pixel editor) show real structure

Keep handy

  • Free APIs that give a small project real data to work with

Also worth your time

Mark this stage as not done?