Export and Import Functions

Dana’s tracker works, but tracker.js now does everything in one file: it reads expenses.json, fixes the types, adds up the amounts, and writes the list back. Soon it’ll also total her spending by category and compare it with a budget. Every new job makes the file longer and harder to find your way around.

In this lesson, you’ll move the loading code into a file of its own and use it from a second file. You’ll learn what the export you kept in the last exercise was doing and why tracker.js starts with an import line.

Move the Loader Into load.js

Splitting a program works only if one file can use code that lives in another. In a Node.js project set up like Dana’s, each file is a module: a file with its own names, which can share some of them with other modules.

The kind you’re writing is an ES module, short for ECMAScript module, the standard module system built into JavaScript.

Your expenses.json should still hold the five records you saved in the last lesson. If yours is missing or different, copy this one:

Language: JSON Filename: 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"
  }
]

In your budget-tracker folder, create a file called load.js next to tracker.js. It holds the reading and writing code from the last lessons, wrapped in two functions:

Language: JavaScript Filename: load.js
import { readFileSync, writeFileSync } from "node:fs";

export function loadExpenses(path) {
  const text = readFileSync(path, "utf8");
  return JSON.parse(text).map((expense) => ({
    ...expense,
    date: new Date(expense.date),
    amount: Number(expense.amount),
  }));
}

export function saveExpenses(path, expenses) {
  writeFileSync(path, JSON.stringify(expenses, null, 2));
}

The export in front of each function makes that name available to other modules. That’s what it did in the exercise, too: the tests were another module, and export let them reach your toJsonText() and fromJsonText().

The other side is import. Next to load.js, create main.js, which takes over from tracker.js. Its first line asks load.js for loadExpenses() by name:

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Become a Member to join the conversation.