Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Set Breakpoints and Debug a Node Application With Chrome DevTools

DZone's Guide to

Set Breakpoints and Debug a Node Application With Chrome DevTools

In this article, you'll learn how to use some built-in tools in Chrome and Node to root out and eliminate bugs in your Node.js application.

Free Resource

Image title

In this post, we are going to learn how to debug a Node application.

Step 1: Please make sure you are running your Node application on a Node version greater than 6.3. If you are using lower version, you can update your node to the higher version.

Step 2: Usually we will run our node application as node index.js. We will also need to use the --inspect flag while running this command.

node --inspect index.js


$ node --inspect index.js 
Debugger listening on port 9229.
Warning: This is an experimental feature and could change at any time.
To start debugging, open the following URL in Chrome:
chrome-devtools://devtools/remote/serve_file/@60cd6e859b9f557d2312f5bf532f6aec5f284980/inspector.html?experiments=true&v8only=true&ws=localhost:9229/f68cd621-bdca-4e84-94ba-5467f8a49945

When you run your application using the --inspect flag, you will get a chrome-devtools URL. Please copy and paste the URL into the Chrome browser to start debugging.

Step 3: Open the chrome://inspect URL in the Chrome browser. You will be able to find your application in the remote target section.



Step 4:
 Open the debugging URL in the Chrome browser. You can find the Node source code in the browser.

In the source tab, you can find the total source code of the application. Now you can set breakpoints and debug your application.

In the above example, a breakpoint is set on line number 7. Hit http://localhost:3000/ (I'm running my application on port number 3000) in another tab and the code will break in line number 7 of the application.

If you want to know more about Node debugging, check out node.js docs.

The Node debugger application will run on default port 9299 and if you want to change the port you can use the following command --inspect=port. Make sure port is available.

If you are using a lower version of Node, try to use the node-inspect module for debugging. However, it is preferable to use the built-in version available with Node.

If you enjoyed this article, Hit Like & please share with your developer friends. Thanks for reading!

This article is originally published from Node Simplified. Take a look into Top Javascript Tips and Tricks.

Topics:
node js ,debugging ,debugging tools ,performance

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}