Using Chrome DevTools to debug frontend and backend JavaScript A console.log intervention Chris Opperwall Blocked Unblock Follow Following Oct 14, 2017 Using debugging tools is an excellent way to understand what is happening in your application. A debugger can help you quickly pinpoint the cause of a bug, and can also give you a more complete snapshot of your program’s state than by using console.log s. If your original hypothesis of why your program is failing is incorrect, you can potentially step further into your program without having to rerun it, which saves you time. Chrome DevTools provides a fairly good debugging experience for JavaScript not only in the browser, but for NodeJS programs that run on the backend. This post aims to show how to debug both types of programs by recreating an actual debugging experience. The Example The project used for this example is called Pulldasher, an open source NodeJS application written by iFixit that serves as a realtime pull request status dashboard. The code used in this example can be checked out at this commit. The Problem At Hand Sometime last week, an engineer at iFixit noticed that there was a display bug on Pulldasher. For background, each Pull Request has a listing on the dashboard, and each listing can contain indicators that show information about the Pull Request. When a Pull Request is being tested, an “eye” icon shows up that has a tooltip with the username of the tester and the day they started testing it. The expected behavior is shown below. The expected behavior However, the tooltip was showing a Date string for the username instead of the tester’s name, as well as an incorrect date for the start time. The bug First Up: Front End Debugging My first thought was that the front end display logic wasn’t correctly accessing the fields from the Pull Request object sent from the backend. In order to test this, I threw a debugger statement where the “eye” icon is added to the page. If you have Chrome DevTools open, it will stop execution at the line that you place the debugger statement. From there you can see the entire call stack so far on the right hand of the window, the location where the program stopped highlighted in blue, and the values of variables declared so far shown in orange/peach next to their uses in the source. You can also use the console to access variables that are currently in scope where the program is stopped. In the console I typed in label to see what the key/value pairs of the object looked like. From what I can see, the value for user is a Date string, the value for created_at is null , and the value for repo is the value that user should be set to. If we need to look further in the source for the bug, we could step through the program using the buttons above the Debugger paused text in the top right. However, at this point we know that the data is wrong, so we have to look elsewhere.

How DevTools looks when the program reaches a breakpoint.

The first attempt didn’t find the bug, but that’s okay because we can add a new breakpoint earlier in the program and see if the problem exists when we get the data from the backend. The place where the frontend receives data from the backend exists in public/js/pullManager.js , so we can click on that file in the file browser on the left and click the line number (in this case line 10) to set a new breakpoint. Reload the page to trigger the breakpoint. Once the program reaches the breakpoint, you can use the console to find the Pull Request object that had the bad label object. From there we can look at the labels field. The console shows that both labels on the pull have the same issue that we saw before, which means that there must be a problem with how label s are being constructed on the backend. We’ll have to setup the NodeJS application to work with a debugger.

The console can be used to inspect the fields of elements that are in scope when the program pauses