Debugging Node.js

Navoda Nilakshi
CodeX
Published in
5 min readDec 24, 2021

--

Bugs are always an inevitable part of development. You never know when your program is going to freak out and throw a big fat error on the screen. That’s why in this article we are going to explore various methods of debugging in Node.js so that you can get unstuck much faster and carry on developing your applications.

In my opinion, client-side Javascript (browser JS) is much easier to debug because of ‘dev tools’ and whatnot. Most of us have fancy editors which allow us to debug easily and painlessly as possible.

Now, with server-side JavaScript AKA Node.js, this is not always the case. Although there is undeniable support from modern IDE’s and editors, there are situations that can get quite frustrating.

There are a lot of ways of debugging in Node.js and in this article we’re going to learn a couple of the most popular ways. There are of course many other ways that can be IDE/code editor specific that comes with your editor. On the other hand, there are also a few ways of universal debugging irrespective of the editor you use.

The ‘infamous’ console.log()

Let me start off by saying this is the most critiqued way of debugging because of its fundamental nature. But nevertheless, there are a lot of situations where a simple ‘console.log()’ can trace what’s wrong in your code and help you to fix it faster.

Consider the following code snippet:

If you run this code, you’ll be getting like so, even when there are no even numbers in the array.

Well, this is because I have ‘mistakenly’ used 1 instead of 2 when taking the modulus. Every number is smoothly divisible by 1 and so the test passes filtering every single number into the new array. This makes its length > 0, and so ‘WE FOUND EVEN NUMS’ is printed out even though in reality every number is odd.

This example is trivial and you can see where we went wrong in just a split second. But imagine this is a big chunk of code doing a dozen things making it hard to trace the place where the code goes crazy. What you can do is, you can locate the place from which the result is printed out, and then you can look into what ‘evens’ array consists of to produce such output by console logging ‘evens’ array.

console.log(evens)

You will get the following output:

Now you can reach the ‘aha’ moment far too quickly as you can see the entire array is printed out despite being not even. You get to know this is something wrong with your logic and you can make it correct in an instance.

console.log()” can help you to dump values of variables into the console so you can have a look to see where you went wrong with your logic. Hopefully, you can see the potential of the simple “console.log()’ statement when debugging. Alright..on to the next!

Node Debugger

This is the next part of the debugging cinema. Adding one or two console.logs to find your way is fine. But when developing large applications (unlike my silly checkEvens example, this can get out of hands very quickly and become more and more obnoxious as you keep on adding console.logs everywhere.

Well, that’s exactly when Node’s in-built debugger comes in handy. You can easily use your browser (Chrome) which is built on top of the v8 JavaScript engine. Yes, you heard that right. You can debug Node with the help of browser!

The first step is to place the ‘debugger’ keyword anywhere that makes sense in your problematic code. Ideally, this should be before decision-making.

Afterward, run the application as usual, but with ‘inspect’ argument.

node inspect app.js

It should leave you like so,

The next step is to open up chrome and go to ‘chrome://inspect’. If you did all the previous steps correctly, your resulting page should look similar to this,

Under ‘Remote Target’, you will get to see the process that you just ran in the terminal. Click the ‘inspect’ link under the relevant target and it will open up your file in ‘dev tools’. But if your folder/file is not opened by any chance, you can always pop it open by going to FileSystem->Add folder to workspace.

If you are all set, you can click the play button on the top right-hand side, to go to the next step in the debugging process. Clicking the play button, in this case, will freeze the execution right when it meets the ‘debugger’ keyword in our code. From there, you can easily inspect the values of variables from the right-hand side panel.

As you can see from the above screenshot, this way is easier, faster, and scalable when it comes to inspecting what values are being carried out by our variables. You can also add breakpoints as you wish to stop the execution at a specific point of your interest.

You can refer to the official Node.js guide on debugging if you need further clarifications.

So there you have it! Try using these methods next time you face one of those scary errors. Until next time!

--

--