Recently i've been learning about Node JS and was hitting a wall when it came to debugging the server side code. It's not that debugging Node is difficult, it just that I ended up trying a lot of things that didn't work which, it turned out, was because I was doing it wrong, and because I didn't have any patience.
I'm an avid Sublime and us it pretty much to do everything text related. And I wanted to keep using Sublime for Node development, rather than use and IDE. And you can.
- Sublime 3 as my text editor
- node-inspector as my debug engine
- Chrome as my node debugger
Assuming you're already set up to develop with Node and Express, and have a new project ready to debug which will serve on port 3000.
Step 1. Install node-inspector globally:
$ npm install -g node-inspector
Step 2. In a new terminal window, launch node inspector:
This will give you the message:
Debug info is server on this port.
In another terminal window, launch your Node project using the debug flag
$ node --debug myApp
This should give you the message:
debugger listening on port 5858 Express server listening on port 3000
If you have some server side code which executes immediately your Node app is launched, and you can't debug it in Chrome, you can launch Node in an immediately debugging mode using:
$ node --debug-brk myApp
Using this method, your server side code will immediately break on launch in your Chrome window and you'll be able to step through it.