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.
npm install -g node-inspector
To demonstrate the usage of debugger let us create a simple Express based web application.
express debugeg cd debueg npm install node app.js
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.
node --debug-brk app.js
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
- Help to debug the code and fix the issues
- Help to understand the underlying node.js code and its functioning
- 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.