Recognize the Common Runtime Errors

In the last lesson, you followed Ruth’s undefined run back to the loop in printSeason() and fixed it there. The season log prints all six runs now. However, Ruth keeps adding to her program, and each change can end in a report that looks different from the one you’ve read.

In this lesson, you’ll meet the error reports you’re most likely to see next. You’ll be able to name each one, say in plain words what it means, and find the function and line where it was thrown. Start with a small change Ruth makes to how each run is printed.

Spot a Value That Isn’t a Function

The reports in this lesson come from Ruth’s season-log project, as you left it at the end of the last lesson. If you don’t have it anymore, open the files below and re-create them in a season-log folder in your home folder:

This package.json tells Node.js to treat the .js files as ES modules:

Language: JSON Filename: package.json
{ "type": "module" }

The first three Saturdays of the season live in runs.json:

Language: JSON Filename: runs.json
[
  { "id": 1, "runner": "Ruth Abebe", "date": "2026-04-04",
    "distance": 5, "minutes": 27 },
  { "id": 2, "runner": "Priya Nair", "date": "2026-04-04",
    "distance": 5, "minutes": 31 },
  { "id": 3, "runner": "Tomás Herrera", "date": "2026-04-04",
    "distance": 8, "minutes": 44 },
  { "id": 4, "runner": "Grace Kim", "date": "2026-04-11",
    "distance": 10, "minutes": 52 },
  { "id": 5, "runner": "Kwame Mensah", "date": "2026-04-11",
    "distance": 5, "minutes": 24 },
  { "id": 6, "runner": "Ruth Abebe", "date": "2026-04-18",
    "distance": 8, "minutes": 45 }
]

The loader in load.js reads and parses that file:

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

export function loadRuns(path) {
  const text = readFileSync(path, "utf-8");
  return JSON.parse(text);
}

The report in report.js has the fixed loop, with < instead of <=:

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const who = `${run.runner} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]));
  }
}

Finally, main.js loads the runs and prints the season:

Language: JavaScript Filename: main.js
import { loadRuns } from "./load.js";
import { printSeason } from "./report.js";

const runs = loadRuns("runs.json");
printSeason(runs);

Ruth wants the runners’ names in capital letters so they stand out on the club’s noticeboard. She adds a line to formatRun() that converts the name and uses the result in who:

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const name = run.runner.toUppercase();
  const who = `${name} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]));
  }
}

Run main.js again, and not a single run prints:

Language: Shell
$ node main.js
file:///Users/you/season-log/report.js:2
  const name = run.runner.toUppercase();
                          ^

TypeError: run.runner.toUppercase is not a function
    at formatRun (file:///Users/you/season-log/report.js:2:27)
    at printSeason (file:///Users/you/season-log/report.js:9:17)
    at file:///Users/you/season-log/main.js:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:569:25)
    at async node:internal/modules/esm/loader:650:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:105:5)

Node.js v26.13.1

The at lines read as before. formatRun() threw the error at line 2 of report.js. The calls from printSeason() and main.js led there. It’s a TypeError again, but the message is new.

Watch out: “is not a function” doesn’t mean you forgot to define a function called toUppercase. It means run.runner.toUppercase gave JavaScript a value, and that value isn’t something you can call.

You can find out what that value is in the REPL. Start it with node, and look at the property before you call it:

Language: JavaScript
> const name = "Ruth Abebe";
undefined
> name.toUppercase
undefined
> name.toUppercase()
Uncaught TypeError: name.toUppercase is not a function
> name.toUpperCase()
'RUTH ABEBE'

A method is a property whose value is a function. Strings have a .toUpperCase() method, with a capital C, but no .toUppercase property. Reading a property that isn’t there gives undefined, without any error. The error comes one step later, when you try to call that undefined value. So the fix is the spelling, .toUpperCase(), not a new function.

Type .exit to leave the REPL. The same message turns up whenever you call a variable or a property that holds something other than a function. A misspelled method is the most common case, but not the only one:

Tell a Missing Name From a Bad Number

Two more error names show up in programs like Ruth’s. Each one points at a different kind of mistake, so the name alone already narrows down where to look. Say Ruth types her new line from memory and leaves out run. in front of runner:

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const name = runner.toUpperCase();
  const who = `${name} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]));
  }
}

This time, Node.js reports a different error name:

Language: Shell
$ node main.js
file:///Users/you/season-log/report.js:2
  const name = runner.toUpperCase();
               ^

ReferenceError: runner is not defined
    at formatRun (file:///Users/you/season-log/report.js:2:16)
    at printSeason (file:///Users/you/season-log/report.js:9:17)
    at file:///Users/you/season-log/main.js:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:569:25)
    at async node:internal/modules/esm/loader:650:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:105:5)

Node.js v26.13.1

A ReferenceError is about a name, and its message says what went wrong with it. Here, runner is not defined means JavaScript couldn’t find a name runner anywhere formatRun() can see. There’s no variable or parameter called runner in formatRun(), so there’s no value at all.

Compare that with “is not a function”, where the property read worked and gave a value, undefined, but not one you can call. The caret sits under runner, the name to check for a typo or a missing run..

The at lines read as in the first report, except that formatRun() now points to column 16 instead of 27. formatRun() threw the error at line 2 of report.js, called from printSeason() at line 9, called from line 5 of main.js. The three lines below that show Node.js starting the module, the same as before, so your own code ends at main.js.

Ruth’s next idea is a tidy column. She pads each name with spaces so the dates line up. " ".repeat(3) gives a string of three spaces, and twelve characters seem enough for every name:

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const gap = " ".repeat(12 - run.runner.length);
  const who = `${run.runner}${gap} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]));
  }
}

Two runs print in a neat column, and then the program stops:

Language: Shell
$ node main.js
Ruth Abebe   (2026-04-04): 5 km in 27 min
Priya Nair   (2026-04-04): 5 km in 31 min
file:///Users/you/season-log/report.js:2
  const gap = " ".repeat(12 - run.runner.length);
                  ^

RangeError: Invalid count value: -1
    at String.repeat (<anonymous>)
    at formatRun (file:///Users/you/season-log/report.js:2:19)
    at printSeason (file:///Users/you/season-log/report.js:9:17)
    at file:///Users/you/season-log/main.js:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:569:25)
    at async node:internal/modules/esm/loader:650:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:105:5)

Node.js v26.13.1

A RangeError means a value has the right type but is outside the range an operation accepts. .repeat() needs a count of zero or more, and it got -1. The third runner, Tomás Herrera, has thirteen characters in his name.

The top at line is new too. String.repeat (<anonymous>) names JavaScript’s built-in .repeat() method, which threw the error. It isn’t your code, so read on to the first frame in your own files: formatRun(), line 2, where the bad count was passed in. Below it come printSeason() and main.js, then the same three Node.js lines that start the module.

Read a SyntaxError Before Anything Runs

The errors so far happened while the program ran, after some lines had already done their work. A SyntaxError in your own code is different. It means JavaScript can’t read the code as JavaScript at all, perhaps because a bracket is never closed. Say Ruth tidies up printSeason() and drops one ) from line 8:

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const who = `${run.runner} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]);
  }
}

The mistake is in report.js, but main.js is the program you run, and its first job is to load the runs. Before you run it, predict how many runs print this time.

No runs print at all:

Language: Shell
$ node main.js
file:///Users/you/season-log/report.js:8
    console.log(formatRun(runs[i]);
                                 ^

SyntaxError: missing ) after argument list
    at compileSourceTextModule (node:internal/modules/esm/utils:355:16)
    at ModuleLoader.moduleStrategy (node:internal/modules/esm/translators:94:18)
    at #translate (node:internal/modules/esm/loader:441:20)
    at afterLoad (node:internal/modules/esm/loader:509:29)
    at ModuleLoader.loadAndTranslate (node:internal/modules/esm/loader:514:12)
    at #getOrCreateModuleJobAfterResolve (node:internal/modules/esm/loader:567:36)
    at afterResolve (node:internal/modules/esm/loader:614:52)
    at ModuleLoader.getOrCreateModuleJob (node:internal/modules/esm/loader:620:12)
    at ModuleJob.syncLink (node:internal/modules/esm/module_job:277:33)
    at ModuleJob.link (node:internal/modules/esm/module_job:389:17)

Node.js v26.13.1

Remember that Node.js loads every module a program imports before it runs any of them. While loading, it reads each file’s code. It couldn’t read report.js, so no line of any module ran, not even loadRuns() in main.js.

Now read the at lines from the top, as before. Each one names a file path starting with node:internal/modules/esm/. These files contain Node.js’s own code for compiling, loading, and linking modules. Node.js shows at most ten at lines, so the list stops at ModuleJob.link, still inside Node.js.

You won’t find formatRun(), printSeason(), or main.js anywhere in that list, because none of your code was running yet. For a SyntaxError in your code, read the file and line number above the code: report.js:8. The caret marks the spot where JavaScript expected a ).

Not every SyntaxError comes from your own code, though. JSON.parse() can throw one while the program runs, when the text it gets isn’t valid JSON, and then the at lines include the calls in your files that led there.

If you’d like to practice telling these errors apart from the message alone, the button below opens a conversation with the mentor. It shows you one error line at a time, and you name the kind of mistake behind it:

Name Each Error

Ruth wants a heading above the season that says how many runs it has. She adds one line to printSeason():

Language: JavaScript Filename: report.js
export function formatRun(run) {
  const who = `${run.runner} (${run.date})`;
  return `${who}: ${run.distance} km in ${run.minutes} min`;
}

export function printSeason(runs) {
  console.log(`Season 2026: ${runs.length()} runs`);
  for (let i = 0; i < runs.length; i++) {
    console.log(formatRun(runs[i]));
  }
}

The program stops as soon as you run it:

Language: Shell
$ node main.js
file:///Users/you/season-log/report.js:7
  console.log(`Season 2026: ${runs.length()} runs`);
                                   ^

TypeError: runs.length is not a function
    at printSeason (file:///Users/you/season-log/report.js:7:36)
    at file:///Users/you/season-log/main.js:5:1
    at ModuleJob.run (node:internal/modules/esm/module_job:569:25)
    at async node:internal/modules/esm/loader:650:26
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:105:5)

Node.js v26.13.1

Read the report from the top, then answer these questions:

  1. What’s the error’s name, and what does its message mean in plain words?
  2. Which function threw it, in which file, and on which line?
  3. Which chain of calls led there?
  4. Does Ruth need to write a function called length()? What would you change?
  1. It’s a TypeError. runs.length has a value, but that value isn’t a function, so the parentheses can’t call it.
  2. printSeason() threw it, in report.js, on line 7: at printSeason (file:///Users/you/season-log/report.js:7:36).
  3. main.js called printSeason() at the top level on line 5. The ModuleJob.run and node:internal lines show Node.js starting the module.
  4. No. .length is a property that holds a number, 6 here, not a method. Drop the parentheses: ${runs.length}.

You can now name the common runtime errors and read each report down to your own line. Next comes the course’s first coding exercise, which runs in your browser.

You write code in the editor on the page, then click Run Tests. Automated tests check your code and show which tests pass, with a message for each one that fails. Run the tests early and often, and read each message and error report for clues, as you did here. If the editor feels cramped, Full screen gives it the whole window.

In the next exercise, Exercise: Fix the Season Log’s Crashes, you’ll repair a few of the season log’s helpers, using each crash’s report to find where the fix goes.

Become a Member to join the conversation.