In the last video, report.js imported Dana’s budget from budget.js, and a limit raised in main.js changed what report.js worked out. Sharing one budget is useful, but it also means a report depends on whatever another file did to that object.
In this lesson, you’ll give each of Dana’s files one clear job: reading and writing the file, doing the math, holding the plan, and wiring the parts together. You’ll decide what each file exports, and you’ll see why report.js works best when it never imports budget.js at all. Start by naming the jobs the tracker already has.
Give Each Job Its Own File
A program with no plan for where code goes grows into one long file again. Each time Dana wants a new feature, someone has to ask where it belongs. A useful test is whether you can say what a file is for in one short sentence. Dana’s tracker has four jobs:
load.jsreads and writesexpenses.json. It’s the only file that touches the disk.report.jsdoes the math on the expenses: totals and what’s left. It touches no file.budget.jsholds Dana’s monthly plan.main.jsis the entry point, the file you run. It connects the others and prints the results.
You already have load.js from the earlier lesson, unchanged, and expenses.json with its five records. If either is missing or different, copy it from here:
expenses.json
[
{
"date": "2026-10-03T00:00:00.000Z",
"category": "groceries",
"amount": 64,
"note": "farmers market"
},
{
"date": "2026-10-05T00:00:00.000Z",
"category": "transport",
"amount": 45,
"note": "bus pass"
},
{
"date": "2026-10-09T00:00:00.000Z",
"category": "utilities",
"amount": 72
},
{
"date": "2026-10-12T00:00:00.000Z",
"category": "fun",
"amount": 38,
"note": "movie night with Malik"
},
{
"date": "2026-10-14T00:00:00.000Z",
"category": "groceries",
"amount": 52,
"note": "weekly shop"
}
]
load.js from the earlier lesson reads and writes that file:
load.js
import { readFileSync, writeFileSync } from "node:fs";
function toRecord(expense) {
return {
...expense,
date: new Date(expense.date),
amount: Number(expense.amount),
};
}
export function loadExpenses(path) {
const text = readFileSync(path, "utf8");
return JSON.parse(text).map(toRecord);
}
export function saveExpenses(path, expenses) {
writeFileSync(path, JSON.stringify(expenses, null, 2));
}
Next to them, create budget.js with the budget from the video. Leave out its console.log() line. It was there to show how often the file runs, and Dana’s tracker doesn’t need it:
budget.js
export const budget = {
month: "2026-10",
startsOn: new Date("2026-10-01"),
limits: {
groceries: 450,
rent: 1200,
utilities: 160,
transport: 90,
fun: 150,
},
};
budget.js exports one name because one thing lives there. Each file’s exports are its answer to “What can other files ask me for?”