Read and Fix Your First Errors

So far, every script you’ve run has worked, or Node couldn’t find the file at all. Sooner or later, though, you’ll leave out a quote or mistype a name, and JavaScript will answer with a block of text instead of your output. That block is called an error message, and it’s JavaScript telling you where it got lost in your file.

In this lesson, you’ll break a small script on purpose, one mistake at a time. You’ll know what each mistake is before you run the script, so nothing here is meant to surprise you. You’ll see what each error looks like, learn which few parts matter, and fix it. By the end, you’ll be able to read an error, say what kind of trouble it describes, and repair a script with more than one mistake.

Start With a Script That Works

You need a working version first. Open your js-course folder in VS Code, create a new file called oops.js in the scratch folder, and type these two lines. They show a movie-night message and the popcorn price from the video Which Lines Print?, with two bags at 3 each.

Language: JavaScript Filename: scratch/oops.js
console.log("Movie night starts at eight!");
console.log("Popcorn costs", 2 * 3);

Save the file and run it from the terminal, with the prompt ending in js-course as usual.

Language: Shell
$ node scratch/oops.js
Movie night starts at eight!
Popcorn costs 6

Both lines show up, so this is your known-good version to come back to whenever an experiment gets confusing.

Leave Out a Quote

On line 2, delete the closing quote after costs. JavaScript will read "Popcorn costs and keep looking for the quote that ends the text, but there isn’t one.

Language: JavaScript Filename: scratch/oops.js
console.log("Movie night starts at eight!");
console.log("Popcorn costs, 2 * 3);

Save and run the same command again.

Language: Shell
$ node scratch/oops.js
/Users/maya/js-course/scratch/oops.js:2
console.log("Popcorn costs, 2 * 3);
            ^^^^^^^^^^^^^^^^^^^^^^^

SyntaxError: Invalid or unexpected token
    at wrapSafe (node:internal/modules/cjs/loader:1900:18)
    at Module._compile (node:internal/modules/cjs/loader:1942:20)
    at Object..js (node:internal/modules/cjs/loader:2108:10)
    at Module.load (node:internal/modules/cjs/loader:1690:32)
    at Module._load (node:internal/modules/cjs/loader:1480:12)
    at wrapModuleLoad (node:internal/modules/cjs/loader:261:19)
    at Module.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:171:5)
    at node:internal/main/run_main_module:33:47

Node.js v26.13.0

Whoops, that’s a lot of text for one missing quote! Notice what’s missing, too. Line 1 is fine, yet Movie night starts at eight! didn’t appear.

Read the Four Parts of an Error

Most of that block is Node describing its own insides, like the Cannot find module error you met when you checked Node. You only need four parts, near the top:

  1. The file and line: The first line ends in oops.js:2, so the trouble is in scratch/oops.js, on line 2. The start of the path depends on your computer. On Windows, it looks more like C:\Users\maya\js-course\scratch\oops.js:2.
  2. The source line: Node copies that line from your file.
  3. The caret: The ^ marks below it point to where JavaScript got lost. Here they run from the opening quote to the end of the line, because JavaScript treated all of it as text that never ended.
  4. The error type and message: The line starting with SyntaxError names the kind of trouble and describes it.

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.