How To Debug Node.js Applications

June 9, 2023 / General Discussion

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

Node.js is a server-side javascript technology and it is very reliable, asynchronous, and event-driven. Moreover, you can locate the code issues with the help of Chrome Dev Tools.

Debugging the Node.js Application:

One of the best methods to spot problems in Node.js applications is the extra usage of console.log debugging.

Now, Check them below:

The debug module

One of the most popular modules that are required in your project comes with a debug module. You can use this module, and you can enable the third part of the module, to log into the standard output, stdout.

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

For using the debug module, first of all, you need to set the DEBUG environment variable while you are starting with applications. You can also use the character wildcard names.

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

DEBUG=express* node app.js

Check out the output given below:

Built-in Node.js Debugger

Talking about Node.js it includes a full-featured debugging utility that can be easy to access through a TCP-based protocol and a built-in debugging client.

To start the built-in debugger, you have to start the application, you have to execute the command given below:

node debug app.js

After this, you have entered this, and you will get 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

Choose any bodHOST’s Nodejs Hosting Server with Ultimate Power, Speed, and one of the best Security!

If you want to 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

Watch the expressions and different variables during the process of debugging. Every expression from the watcher’s list will be evaluated in the current context on each breakpoint and shown just before the source code listing for the breakpoints.

If you want to use the watchers, then you need to define them for the expressions you want to watch. Follow these steps to put this into action:

watch (‘expression’)

The Chrome Debugger

When you debug complex applications, visuals can help. You can use the familiar UI of the Chrome DevTools for debugging the Node.js applications.

The best part of the Chrome Debug protocol has been ported into the Node.js module and it can be ideally used for the debugging of the Node.js applications. For this, you need to install the node inspector:

npm install -g node-inspector

Start the debugging your applications by doing it in the following way:

node-debug index.js –debug-brk

You may start debugging your Node.js applications with it, and it will expand the Chrome Developer tools.

Conclusion

In this way, we have seen how to debug a remote application using Chrome DevTools. With the help of watch variables and breakpoints, you can simply follow errors without publishing to the console and see how the variables change.

Leave a Reply

Your email address will not be published. Required fields are marked *