Knowledge Base Hub

Browse through our helpful how-to guides to get the fastest solutions to your technical issues.

Home  >  Web Hosting FAQ  >  How To Debug Node.js Applications?
Top Scroll

How To Debug Node.js Applications?

 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.

For our Knowledge Base visitors only
Get 10% OFF on Hosting
Special Offer!
30
MINS
59
SECS
Claim the discount before it’s too late. Use the coupon code:
STORYSAVER
Note: Copy the coupon code and apply it on checkout.