Node Inspector for Debugging Node.js application

Bugs are often a part of software development and as developers we often need good tools to debug and fix these bugs effeciently. Node Inspector is one of the power debugging tools available for node.js.

Let us install this debugger globally so it will be available to all nodejs application in the system.

[code]
npm install -g node-inspector
[/code]

To demonstrate the usage of debugger let us create a simple Express based web application.

[code]</pre>
express debugeg
cd debueg
npm install
node app.js

[/code]

We will now able to view the application via  http://locahost:3000

Once we make sure that the default application is working, restart it in debug mode as mentioned below.

[code]
node –debug-brk app.js

[/code]

Node Inspector will put a breakpoint in the first line of the application, when we use –debug-brk. On successful startup of the application in debug mode, node will display the following message “debugger listening on port 5858

Now start the Node Inspector by typing the command “node-inspector”. System will display the following message on successful startup of node insepector as

 info – socket.io started . visit http://0.0.0.0:8080/debug?port=5858 to start debugging

Now open the above URL : http://0.0.0.0:8080/debug?port=5858  in Chrome. The URL will work only in WebKit JS debugger supported browsers. The node inspector will display the application code in a debug window where we can step through the code and understand the behaviour of the application.

Advantages of Node Inspector

  1. Help to debug the code and fix the issues
  2. Help to understand the underlying node.js code and its functioning
  3. Reading the code can be more interactive using node-inspector debug window.

We can also use the V8 debugger to debug the application, which we will be done through command terminal window. However it is the developer comfort to decide which tool to use.

Posted in: Programming

Leave a Comment