Skip to content
Learn Netverks

Lesson

Step 24/36 67% through track

file-json-storage

File and JSON storage

Last reviewed May 28, 2026 Content v20260528
Track mode
nodejs_server
Means
Node sandbox
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches File and JSON storage: the syntax, APIs, and habits you need before advancing in Node.js.

Teams ship File and JSON storage on every Node.js codebase—skipping it leaves gaps in debugging and code reviews.

You will apply File and JSON storage in contexts like: REST/GraphQL APIs, BFF layers, CLIs, webhooks, and real-time services (with WebSockets).

Run JavaScript on the Node runner when configured—never mix arbitrary shell commands in lessons.

When you can explain the previous lesson's ideas without copying starter code.

Small apps and prototypes store data in JSON files on disk—simple but not concurrent-safe at scale. Learn read-modify-write patterns before reaching for MongoDB or PostgreSQL.

Read and write JSON

import { readFile, writeFile } from 'node:fs/promises';

async function loadDb(path) {
  try {
    return JSON.parse(await readFile(path, 'utf8'));
  } catch (err) {
    if (err.code === 'ENOENT') return { items: [] };
    throw err;
  }
}

async function saveDb(path, data) {
  await writeFile(path, JSON.stringify(data, null, 2));
}

Limitations

Race conditions when two requests write simultaneously—use file locks, a database, or a queue for production. Good for CLI tools and learning.

Important interview questions and answers

  1. Q: When is JSON file storage OK?
    A: Prototypes, single-user tools, config caches—not high-write multi-tenant APIs.
  2. Q: Atomic writes?
    A: Write to temp file then rename—reduces partial writes on crash.

Self-check

  1. What error code means file missing?
  2. Why pretty-print JSON with null, 2 in dev only?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • JSON.parse try/catch?
  • Atomic write pattern?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump