concept debugger in category javascript

appears as: debuggers, debugger, debugger, The debugger
Node.js in Practice

This is an excerpt from Manning's book Node.js in Practice.

Technique 90 Using Node’s built-in debugger

Whenever you need step-by-step analysis of the state of your application, a debugger can be an invaluable tool, and Node’s built-in debugger is no exception. Node’s built-in tooling allows you to watch variables, pause execution through breakpoints, step in and out of parts of your application, see backtraces, run an interactive context-aware REPL, and more.

debug> restart
program terminated<
debugger listening on port 5858
connecting... ok
Restoring breakpoint debug.js:8
break in start.js:1
  1 var a = 0;
  2
  3 function changeA () {
debug>

Our application now lets us know that the debugger is listening on port 5858. If we refresh our Node inspector web page, it’ll look more interesting, as shown in figure 11.3.

Figure 11.3. Node inspector connected to the debugger
Third-Party JavaScript

This is an excerpt from Manning's book Third-Party JavaScript.

Finally, you must remember that—at their core—third-party JavaScript applications are still ordinary web applications. So in addition to problems specific to third-party scripts, you still have to deal with all the issues affecting normal web application development: old browsers with poor debugging tools, plugins that change the browser’s default behavior, and of course Heisenbugs that are only reproducible in production and not when you’re pointing a debugger at them.

In the dark days of JavaScript programming, developers had only one way of debugging JavaScript code—through good ol’ alert messages. Compared to debugging tools available for other major programming languages, JavaScript used to have the most terrible development platform—bar none. Fortunately, browser vendors realized that JavaScript developers needed better tools, and today all modern browsers come with amazingly powerful built-in inspectors, profilers, and debuggers. In this section, we’ll concentrate on debuggers—programs that let you examine JavaScript code while it’s executing and use them to step through the live code.

Because you have absolutely no idea what’s causing the failure, it’s a good strategy to pause at the beginning of the function and see what happens. There are two ways to place a breakpoint: using the debugger’s UI or placing the special debugger keyword inside your JavaScript code. When using the latter, you need to insert it just before the line on which you’d like to pause the execution:

Now you have to inspect whether your input data is correct—because if it isn’t, you’ll have to go back and put a breakpoint earlier in the code. In this case, the data happens to be valid, so you’ll have to dig deeper into this function in order to find the source of the bug. You’ll use the debugger to step through the code until the line where the message variable is created.