Knowledge Base
MilesWeb / Web Hosting FAQ

How To Debug Node.js Applications?

Approx. read time : 3 min

Node.js is a highly robust, asynchronous and event-driven server-side Javascript technology. And, with Chrome Dev Tools, you can find errors in your code.

In this article, we will get to know the steps to debug your Node.js applications with the help of the debug module, the built-in Node debugger and Chrome’s developer tools.

Debugging the Node.js Application:

One of the best ways to detect issues in Node.js applications is the excessive usage of the console.log for debugging.

Check them below:

The debug module

The most popular modules that are required in your project come with a debug module. Using this module, you can enable the third part module, to log into the standard output, stdout.

To check whether a module is using it, double-check the package.json file’s dependency section.

For using the debug module, you need to set the DEBUG environment variable when you are starting your applications. You can even use the character to wildcard names.

The below line will print all the express-related logs to the standard output.

DEBUG=express* node app.js

You will see the output below:

Built-in Node.js Debugger

Node.js includes a fully-featured debugging utility that can be easily accessed through a TCP-based protocol and a built-in debugging client.

To start the built-in debugger, you need to start the application by entering the command given below:

node debug app.js

After you have entered this, you will get to see the output, as given below:

Descriptions of the Node.js Debugger:

c =continue to execute the code

n=execute this line and go to the next line

s= step into this function

o=finish function execution and step out

repl=allows code to be evaluated remotely

Pick A MilesWeb’s NodeJs Hosting Server Ultimate Power, Speed And Top-Class Security!

You can even add breakpoints to the application by inserting the debugger statement into your codebase.

function add (a, b) {

  debugger

  return a + b

}

var res = add(‘abc’, 3)

The Watchers

You can watch the expressions and variables during the process of debugging. On each of the breakpoints, every expression from the watcher’s list will be evaluated in the present context and displayed immediately before the breakpoints source code listing.

To use the watchers, you need to define them for expressions you need to watch. To do so, you need to do it in the below way:

watch (‘expression’)

Now, to get a list of active watchers, type watchers, to unwatch an expression:

unwatch (‘expression’)

The Chrome Debugger

When you debug complex applications, visuals can help. It would be good to use the familiar UI of the Chrome DevTools for debugging the Node.js applications.

The best part is, the Chrome Debug protocol has been ported into the Node.js module and can be ideally used for debugging the Node.js applications.  To do so, you need to first install the node-inspector:

npm install -g node-inspector

You can start debugging your applications by doing it in a below way:

node-debug index.js –debug-brk

It will extend the Chrome Developer tools, and you can start to debug your Node.js applications with it.

Conclusion

In this article, we have seen how to debug a remote application using Chrome DevTools. In the DevTools, you can see the variable changes and track bugs easily without printing to the console with the help of the watch variables and breakpoints.

Nehal is an ardent content writer. She's passionate about crafting content that's simple but adds value. Her insatiable interest in writing has allowed her to explore her skills. She is adept and can write for different types of content formats.

Trusted By Thousands of Clients & Big Businesses

We highly appreciate the kind and stellar feedback we receive from our customers. Delivering the best is our goal! MilesWeb is rated Excellent out of 5 based on reviews. Read more reviews.

Excellent
Based on reviews
2 hours ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
4 hours ago
Milesweb is superb Hosting pro...
Milesweb is superb Hosting provider ever, their Support team is amazing!!!...
Abhishek Singh
15 hours ago
Great support in great timing...
We need urgent assistance on changes in a primary domain on our client's Cpanel accounts and reached...
Riyaju Deen
21 hours ago
Best Website Hosting platform ...
I was new on MilesWeb. And needed help on multiple areas from setting up to getting started with cre...
Harshada
1 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
1 days ago
the team is very supportive th...
the team is very supportive though at times effort needs to be made to make understand the problem s...
Suree Sharma
1 days ago
I am using miles web for 3plus...
I am using miles web for 3plus years, very quick and perfect support by the team, they helped me man...
Sri Raghav
2 days ago
The service is good...
The service is good. They are answering with patience and doing the needful as soon as possible....
MR
2 days ago
Perfect and Valuable Server + ...
I am using MilesWeb Servers, The main thing which I getting are continuous support over everything w...
Gunjan Makwana
3 days ago
Very quick and helpful assista...
Very quick and helpful assistance. Support person listened properly and provided a nice solution....
Narendra
4 days ago
positively helped me with find...
positively helped me with finding insecure content on my website causing SSL to not work properly on...
Thaviraj Junglee
4 days ago
Exceptional support, Truly Pra...
I had opted for the basic wordpress hosting plan as I intended to experiment with various plug-ins. ...
Aseem Chandna