Node.js lesson · 34 min

Node.js Filesystem Basics

Read JSON from disk with `node:fs/promises`, parse it, and handle file errors.

Code runs locally in your browser

What you will practice

  • Understand filesystem access as a Node.js feature.
  • Read fake file contents safely in the lesson.
  • Parse file-like JSON data.

What this means

Unlike browser JavaScript, Node.js can read and write files on the machine where it runs.

That power is useful for scripts, logs, uploads, build tools, and configuration.

It is also sensitive: server code must validate paths and never let users read arbitrary files.

If this is your first time seeing this

Node.js can read and write files, which browsers cannot freely do.

That power is useful, but file paths must be handled carefully.

Mini glossary

Filesystem
The files and folders available to a program.
Path
A string that points to a file or folder location.
JSON config
A settings file written as JSON.

Example from everyday life

Filesystem access is like having keys to a filing cabinet. Useful keys help you get the right folder quickly, but careless keys can open drawers nobody should touch.

How it works step by step

  1. Create a `data` folder and a JSON file.
  2. Use `readFile` from `node:fs/promises` to read the file asynchronously.
  3. Parse the text with `JSON.parse`.
  4. Handle errors with `try/catch` so the script fails clearly instead of crashing mysteriously.

Where you will use this

  • A build script reads a config file.
  • A CLI tool loads input from a local file.
  • A backend stores uploaded files after checking size and type.

Before you run the code

Modern Node.js code often imports built-in modules with the `node:` prefix, such as `node:fs/promises`.

Never build file paths directly from untrusted user input without strict validation.

Run it locally

These examples are meant for a local Node.js project. Create the files shown below, then run the terminal commands from the project folder.

Create folders and files

mkdir lumio-fs-demo
cd lumio-fs-demo
mkdir data
touch data/settings.json read-settings.js

JSON input file

data/settings.json
{
  "siteName": "Lumio Utils",
  "theme": "dark",
  "features": ["tools", "learn", "analytics"]
}

Read and parse the file

read-settings.js
const { readFile } = require("node:fs/promises");

async function main() {
  try {
    const raw = await readFile("./data/settings.json", "utf8");
    const settings = JSON.parse(raw);

    console.log(`Site: ${settings.siteName}`);
    console.log(`Theme: ${settings.theme}`);
    console.log(`Features: ${settings.features.join(", ")}`);
  } catch (error) {
    console.error("Could not read settings:", error.message);
    process.exitCode = 1;
  }
}

main();

Run it

node read-settings.js

Common beginner mistakes

  • Letting users choose arbitrary server file paths.
  • Assuming a file always exists.
  • Parsing JSON without handling invalid JSON errors.

Why there is no browser runner here

This lesson uses real Node.js APIs and terminal commands. Run the examples locally with Node.js so you learn the same workflow used in actual backend projects.

Try changing this next

  • Change the file path and watch the error.
  • Add another setting to `data/settings.json`.
  • Wrap `JSON.parse` in try/catch.