Locked learning resources

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

Unlock This Lesson

Locked learning resources

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

Unlock This Lesson

Reading a Stack Trace: the Traceback Upside Down

Reading a Stack Trace: the Traceback Upside Down

In this lesson, you read the error JavaScript prints when a script stops and found the mistake in it, starting from the top.

  • A Python traceback puts the most recent call last, so you read it from the bottom. A JavaScript error puts it first, so you read it from the top.
  • The first lines show the file and line number, the line itself with a caret, then the error’s name and message.
  • Each at line below is one call. The first is where the error was thrown, with a line and a column number. Each line under it is the caller of the one above. An at line with no function name is the top level of the file, like Python’s <module>.
  • Stop reading where the frames start with node:, such as node:internal. They’re Node loading your module, not your code.
  • A ReferenceError is JavaScript’s NameError. Calling a method that doesn’t exist is a TypeError, where Python says AttributeError. A SyntaxError means JavaScript couldn’t read the file, so none of it ran.

The team’s check now keeps its report in a function. In JavaScript, function plays the role of def, and braces hold the body where Python uses indentation. Functions get a course of their own later.

Here’s the port with one sys.platform left over from the Python:

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

00:00 In this lesson, you’ll read the error JavaScript prints when a script stops, and find your mistake in it. The team’s Python check now keeps its report in a function, print_report(), and calls it at the bottom of the file.

00:14 Your port does the same. JavaScript’s function keyword plays the role of def, and braces hold the body, where Python uses indentation. That’s all you need about functions for now: they get a course of their own later.

00:28 Porting fast, you changed every print, but one line still says sys.platform.

00:36 Now run the port with node. Python puts the most recent call last; Node puts it first, so start at the top. JavaScript puts it at the top. One report line prints, and right under it, your line appears with a caret under sys.

00:53 Read it from the bottom, the Python way, and you meet Node’s version number, then three lines that point into node internal. None of them is your code: they’re Node loading your module.

01:07 So read Node’s output from the top. The first line names the file and the line number, three. Under it is the line itself, with a caret where it failed. Then come the error’s name and its message.

01:22 As when the module stopped at folder, a ReferenceError is JavaScript’s NameError, and both messages say sys is not defined.

01:32 Below the message, each at line is one call. The first is where the error was thrown: inside printReport(), at line three, column twenty-two. Each line below is the caller of the one above.

01:45 Line ten has no function name: it’s the top level of the file, Python’s module line. That’s Python’s order, flipped. The most recent call comes first. Below your file, the frames turn into node internal, so stop reading there.

02:04 The top told you all you needed: line three, sys, not defined. Swap sys for process, as on the other lines, and run it again. The whole report prints, and the reminder after it.

02:20 You’ll meet two more error names soon. Had the port kept a Python comment, starting with a hash, the file stops with a SyntaxError.

02:30 JavaScript couldn’t read the file, so nothing ran, not even the report above the comment. Every frame is Node’s, and JavaScript comments start with two slashes.

02:41 And calling a method that isn’t there gives a TypeError, where Python says AttributeError.

02:49 So here’s the rule. Read a JavaScript error from the top: your line, the error, then the calls from the innermost out. Stop where Node’s own frames begin.

03:01 Next up is the text lesson No AttributeError: undefined and the Crash That Comes Later, where a misspelled name after a dot gives you no error at all, and the crash comes lines later.

Become a Member to join the conversation.