Learn About Debugging Node.js Apps

Debugging is at the center of every software developer's armoury when it comes to detect any kind of abnormalities in an application. And thanks to some great IDE's (Integrated Development Environments) these days, this job gets even more simpler and developer friendly.

Debugging is at the center of every software developer's armory when it comes to detecting any kind of abnormalities in an application. And thanks to some great IDE's (Integrated Development Environments) these days, this job gets even simpler and more developer friendly.

For example, any developer working on Microsoft stack would know how well Visual Studio has done in this regard over the past decade or so with each update rolling out something new that goes a long way in assisting developers and making their lives a tad easier at work.

But what about the relatively new kid on the block, Visual Studio Code? Although Visual Studio does offer support now for developing rich client-side apps leveraging some popular frameworks/libraries (Angular, React), VSCode still seems to be the preferable IDE for designing javascript apps given its lightweight nature and obviously being very nice on your system's resources at the same time. 

In this blog, we are going to look into a couple of ways to go about debugging Node.js apps. 
  1. Debugging in VSCode
  2. Debugging via Google Chrome
Just to get a bit of  perspective, given below in the screenshot is a simple Node.js app that we shall be debugging. (code snippet provided)
  1. const books = require("./books")  
  2. var book = {  
  3.     title: "Debugging Node.js apps",  
  4.     description: "How to debug node.js apps"  
  5. }  
  6. console.log("Writing File...");  
  7. books.WriteFile(book);  
  8. console.log("Writing File completed...");  
  9. console.log("---------------------------------");  
  10. console.log("Reading file started...");  
  11. var obj = books.ReadFile();  
  12. console.log(`One book found : - Title : ${obj.title}, Description : ${obj.description}`);  
  13. console.log("Reading file completed...");  
Node.js

In this application, we are performing a simple file read/write I/O operation. The nature of the application doesn't really matter here as we are simply going to dive deep in the debug operation. The two functions here, namely ReadFile() and WriteFile(), are defined in a separate books.js file as shown below (code snippet provided),
  1. const fs = require("fs");  
  2. const WriteFile = (book) => {  
  3.     debugger;  
  4.     fs.writeFileSync("books.json", JSON.stringify(book));  
  5. }  
  6. const ReadFile = () => {  
  7.     var book = fs.readFileSync("books.json");  
  8.     return JSON.parse(book);  
  9. }  
  10. module.exports = {  
  11.     WriteFile,  
  12.     ReadFile  
  13. }  
Node.js
 
Let's get started and have a look at the first approach to debugging 

Debugging in VSCode 
  • Open the terminal in VSCode. Press Ctrl + '`' (backtick - located just below the Esc button)

  • Type the following command - node inspect app.js (app.js is the file that we are debugging) 

    Node.js

  • You will see the following text come up on your terminal screen. The green highlight on the word 'exports' indicates that currently the debugger is placed at that line of code. (if that doesn't cut the deal for you, the '>' pointer at the extreme left of that line can make you relate somewhat!!)

  • The 'debug>' indicates that currently, the debugger process is running in the terminal.

  • Type 'n' and press enter to move to the next piece of code.

  • In case your code has a long execution path and you directly want to jump to the code where you feel you need to debug, you can attach a debugger statement before that.

    Node.js

  • In case you directly want to continue to the statement where you have placed the breakpoint, type 'c' and press Enter. 

    Node.js

  • Similarly for step-into and step out functionality, you can type 's' and 'o' commands respectively 

  • Now, let's say we need to check what's there inside the book object that gets passed to the WriteFile() function.

    To achieve this, we can enable the repl mode of the debugger. repl stands for Read Evaluate Print Loop which broadly performs these four functions internally. When you enable repl mode and type in any genuine object that you want to evaluate like in this case, the book object, it will read the object, evaluate it in a form of a JSON object and finally print it on the screen till the developer simply does a ctrl+ C to exit the repl mode.

    Node.js

  • To exit the debug operation, simply type Ctrl + C and hit enter.
Debugging via Chrome

Google Chrome also provides you an option to leverage its debugger for node.js apps. Let's have a look at the same,
  • Open the terminal (Ctrl + `) 
  • Type the following command - node --inspect-brk app.js

    Node.js

  • Now open Google Chrome and type chrome://inspect to launch the DevTools window.

    Node.js

  • Click on "Open dedicated DevTools for Node" .

  • A window will get displayed with the app.js file opened and a debugger attached to it. You can now simply use F10 or F11 keys to perform the vanilla debugging as done in other IDE's. In the console window below, you can inspect any specific object by typing it in the window.

    Node.js
There we go!

We have looked at a couple of ways to debug node.js apps and of course, there are a lot more ways to achieve the same. Feel free to share your versions and I'll be happy to see them below.

Until next time, Happy Coding!