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

Pausing at a Line With node inspect

Pausing at a Line With node inspect

Joan’s November export has three bad rows, and the Python and JavaScript imports print the same four lines about them. Line 3 of the export says the amount is not a number, but what did the amount field actually hold? In Python, you drop breakpoint() into parse_row(), right after the amount is converted, and run the script as usual:

Language: Python
    except ValueError as err:
        raise BadRowError(line, donor, "amount is not a number") from err
    breakpoint()
    if amount <= 0:

You’re at pdb’s prompt, on the breakpoint line, where p and n work as usual:

Language: Shell
$ python donations.py november.csv
> /Users/you/donations/donations.py(31)parse_row()
-> breakpoint()
(Pdb) p amount_text
'50.00'
(Pdb) p amount
50.0
(Pdb) n
> /Users/you/donations/donations.py(32)parse_row()
-> if amount <= 0:
(Pdb)

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 Joan’s November export has three bad rows, and both imports report them in the same four lines. Line 3 of the export says the amount is not a number. But what did the amount field hold?

00:14 In Python, you’d reach for breakpoint() in the function, right after the amount is converted. Here, you pause the JavaScript port at the same spot to find out.

00:26 Run the script as usual, and you’re at pdb’s prompt, on the breakpoint() line. amount_text is the string '50.00', and amount is the float 50.0.

00:40 One n, and it’s on the next line.

00:44 In the JavaScript port, the matching line is a debugger statement. debugger is a keyword, not a function call. It goes in the same spot, after Number() converts the amount.

00:57 Now run the port as usual, with node. What does the terminal show? Your turn: pause the video and predict.

01:10 The report. The same four lines as before, and the exit code is one.

01:19 Nothing paused, and there’s no error or warning. JavaScript ran past the debugger statement each time a row reached it. A debugger statement pauses only when a debugger is attached to the program.

01:34 Without one, it does nothing. And the node command on its own doesn’t attach one. That’s where the likeness to breakpoint() stops.

01:45 Python’s breakpoint() starts pdb by itself. JavaScript’s statement only stops for a debugger that’s already there. So the fix isn’t in the code: you start the program under a debugger.

02:01 Node’s terminal debugger comes built in. In your donations folder, put inspect between node and the file name. node inspect starts your program as a second Node process, with a debugger port open on your own machine, under a random id.

02:18 Angle-bracket lines are what that process prints: Node’s messages now, your report later. The debugger connects and attaches, then stops on line one, Break on start, as python -m pdb does and breakpoint() never does.

02:35 Type cont, short for continue, and the program runs. It stops at your debugger statement, line 46 of the script, and lists the lines around it, with an arrow on the paused line.

02:49 exec, or p, runs an expression in the paused function, as p does in pdb. exec amountText gives the string '50.00'.

02:59 exec amount gives the number 50.

03:06 next runs one line and stops at the amount check, line 47. cont runs to the statement again. To see which line of the export it’s on, exec an array of three variables.

03:18 Line 3, donor D-108, and the amount field held 'n/a'. Watch out: keep typing cont, and it stops on lines 5, 6 and 7 of the export, never on line 4, whose field check throws: no stop means the code never ran.

03:39 .exit ends the program at once: no question, no report. pdb’s c, n and s become cont, next and step, and the letters still work.

03:52 For pdb’s r, use out, or o, which steps out to the caller. Typing r here restarts the program.

04:02 watch() shows an expression at every stop, in any call; display, only when it changes in the same call. And pdb’s interact becomes repl, a read-eval-print loop in the paused code.

04:18 So a JavaScript developer doesn’t count on a debugger statement to stop a normal run. They start the program under a debugger, where your debugger statement marks the stop, and it comes out before you commit.

04:32 Next up, Debugging in Chrome DevTools and VS Code, a text lesson. You’ll pause in a graphical debugger, set breakpoints with a click, and stop only on the row you want.

Become a Member to join the conversation.