KahWah.me

Java | .NET | Android | iOS | Tridion

Debugging NodeJS applications with Chrome

Node-inspector is a debugger tool that allows you to set breakpoints and inspect symbols in your NodeJS application, the same way as you would do in the Chrome developer tools (or web inspector). In this blogpost I want to show you how.

Until recently NodeJS debugging meant placing a lot of console.log statements in my javascript files. My whole terminal window would fill up with logging statements that after a while wouldn’t be easy to read. It’s a troublesome workflow too, because you have to restart your application after each modification.

If you want to set breakpoints and inspect symbols there is an excellent tool available that allows you to do it in a Chrome developer tools like interface. Node-inspector is an easy to use tool that provides a web inspector view showing you all your Javascript code and makes debugging NodeJS applications a breeze.

To install node-inspector with NPM, fire up a terminal and enter the following

$npm install -g node-inspector

After that you’ll need to start your application in debug mode. If you have a nodejs application that starts fine and want to attach the debugger after starting use the following:

$node --debug yourApplication.js

If however your application does NOT start properly or ends before you have time to attach the debugger, use the following to start your application:

$node --debug-brk yourApplication.js

This will not start the application yet, but wait till you’re ready to push the “run” button in the interface.

Now that the application is running, it’s time to start node-inspector

$node-inspector

You can now launch a Chrome browser go to http://127.0.0.1:8080/debug?port=5858 to see the interface. From here on you can see all the loaded Javascripts on the left. If you launched your NodeJS application with the --debug-brk flag, you can press F8 or the resume button on the top right to resume the application.

Node inspector

Tags: