Whenever I talk to people about node.js, I commonly run into the developer who thinks it's great until they need to debug it. The command line stuff is fairly easy to use:
node --debug myApp.js
But this leads you down a road that we have not traveled in years: command line debugging.
However, this is not the way I ever debug my node.js applications and you shouldn't either. There is a great package called node-inspector that'll help you in your quest to debug node.js applications. It's simple to install:
npm install -g node-inspector
Don't forget the -g, because we'll want to install this globally. The install should only take a few minutes, and then you're ready to debug. Let's run the next two commands:
> node --debug myApp.js debugger listening on port 5858
> node-inspector visit http://0.0.0.0:8080/debug?port=5858 to start debugging
Your application should now be running in debug mode. Pop open a web browser and navigate to http://localhost:8080/debug?port=5858. It should look something like this:
If you're an avid web developer, start jumping for joy. You know exactly what this looks like (CHROME!!!). You can load your scripts by clicking on the small arrow on the left side of the screen.
On this view, you can set breakpoints by clicking on the line numbers. They'll automatically break whenever they're hit.
You'll see on the right side of the screen you can watch variables and look at the call stack. This is a fantastic experience compared to the command line.
Do you need to debug the first couple lines in an app, or can't get into the debugger quick enough to set breakpoints? Try this:
> node --debug-brk myApp.js
This causes your app to auto break on the first line.
There you have it! The mystery of node.js debugging has been revealed and it's easier than you probably thought it could be. Use this as an excuse to try node.js today!