Debugging using Node Inspector

From wiki.gpii
Jump to: navigation, search

This page explains how to install and use node-inspector to debug.

Prerequisites

You should have node.js installed already (including npm) - if not you can find it at www.nodejs.org.

Installation

The installation procedure is very simple using npm (Node Package Manager). After installing Nodejs, fire up a console and enter:

npm install -g node-inspector

Note: On Linux, you may need to run

sudo npm install -g node-inspector

Usage

Debugging with node inspector requires 3 steps:

  1. Starting up node-inspector
  2. Running the gpii using the special --debug or --debug-brk flags
  3. Launching Google Chrome, Safari or Opera, and pointing it to the debugging location

Starting up node-inspector:

Open up a console and type:

node-inspector

You should get an output along the lines of

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

What this does is to fire up node-inspector, which will take your node application (GPII) and make it available to the chrome debugger

Running the gpii using the special --debug or --debug-brk flags

To run the GPII with debug mode on, launch it with the "--debug" or "--debug-brk" flags. "--debug" will turn debug mode on. "--debug-brk" will turn debug mode on and break as the first thing.

So, go to the directory from which you would usually run "start.cmd" (windows) or "start.sh" (linux) from - that is the linux or windows directory within 'gpii' - depending on which OS you're running. In that directory type:

node --debug gpii.js

to run normal debugging.. If you want the debugger to break straight away instead, enter:

node --debug-brk gpii.js

Launching a Webkit-based browser and pointing it to the debugging location

Now we're ready to debug. Fire up your favorite Webkit browser (Opera, Safari or Chrome) and enter the following URL:

http://127.0.0.1:8080/debug?port=5858

This will open up the debugger. The debugger is identical to Chrome's built-in debugger and very similar to Firebug.

  • You can add breakpoints by clicking on the line numbers to the left of the code in the main area.
  • You control the flow of the debugger (run, pause, step into, step out, evaluate, etc.)
  • You can choose the file to look at by clicking the file name immediately above the code in the main area
  • Along the write, you can find information about variables
  • Mousing over variables will give you information about them as well
  • At the bottom of the screen, you can enable the console, to type in your own JavaScript code

Problem solving

  • Sometimes the debugger seem not to load at all (loading a blank page) -- usually restarting the "node-inspector" (killing and starting the program in the console) helps. It could also be Chrome, so restarting that might also solve the problem.
  • If some of the files you require aren't in the list of files, make sure that the statements in which they are 'require'd have been run. Then refresh the window (F5 in most browsers) - don't worry - your breakpoints, etc., won't get lost.