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:02 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:18 Your port does the same. JavaScript’s function keyword plays the role of def, and braces hold the body, where Python uses indentation.

00:27 That’s all you need about functions for now: they get a course of their own later. Porting fast, you changed every print(), but one line still says sys.platform.

00:41 First, the picture you know. Make the same slip in Python, using sys without importing it, and Python stops with a traceback. You read it from the bottom.

00:54 The last line names the error, a NameError. Right above it is the line that failed, with carets under sys. Above that is its caller, line fifteen.

01:06 Python lists the calls with the most recent one last.

01:12 Now run the port with node. Python put the failing line at the bottom, right above the error. Where will JavaScript put it? Your turn: pause the video and predict.

01:28 At the top. One report line prints, and right under it, your line appears with a caret under sys. Read it from the bottom, the Python way, and you meet Node’s version number, then three lines that point into node:internal.

01:45 None of them is your code: they’re Node loading your module.

01:51 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.

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

02:17 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.

02:29 Each line below is the caller of the one above. Line ten has no function name: it’s the top level of the file, Python’s module line. That’s Python’s order, flipped.

02:42 The most recent call comes first. Below your file, the frames turn into node:internal, so stop reading there.

02:53 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.

03:04 The whole report prints, and the reminder after it.

03:09 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. JavaScript couldn’t read the file, so nothing ran, not even the report above the comment.

03:26 Every frame is Node’s, and JavaScript comments start with two slashes.

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

03:40 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. 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.