If you use a virtual machine to develop your Node.js app and probably you use Vagrant to configure and port your work environment, sooner or later you are going to need to debug.While node has a built in debugger, node-inspector provides a pleasant graphical interface for debugging node programs. Node Inspector is a debugger interface for node.js using the WebKit Web Inspector, the familiar javascript debugger from Safari and Chrome.
Installation
With npm:
npm install -g node-inspector
Forwarding ports
– The straight forward option is to open VirtualBox and go to the machine configuration settings.
Virtual box machine settings
And then to Network -> advanced-> port forwarding:
Virtual box port forwarding
There you use VirtualBox’s Port Forwarding Rules window to forward ports. You don’t have to specify any IP addresses. Changes take effect immediately. — If you are using Vagrant, it’s easier. Write down this line in your vagrantfile (I use 3001 as an example)
config.vm.network :forwarded\_port, guest: 3001, host: 3001 # node-inspector
Chages will take effect next time you start your virtual machine.
Debugging with node-inspector
Now you are all set to start using node-inspector. Go to your virtual machine console and run
node --debug your\_program.js
NOTE: make sure that the –debug flag comes before your/node/program.js or else you may see an EADDRINUSE error. and then, in another console, run
node-inspector --web-port=3001
I use 3001 just as an example, you could use whatever you want. Now open http://127.0.0.1:3001/debug?port=5858 in your favorite WebKit based browser. You should now see the javascript source from node. If you don’t, click the scripts tab. Select a script and set some breakpoints (far left line numbers) or simply add a debugger call in your code (node will break automatically on the call, just as V8 does).
Originally published at leolanchas.com on March 15, 2014.
Top comments (0)