DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Leo Lanchas
Leo Lanchas

Posted on • Originally published at Medium on

Debugging a Node.js application, with node-inspector, within a virtual machine.

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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)

πŸ€” Did you know?

Β 
🌚 Dark mode is available in Settings.