If you ask me on which task I've spent most of my time as a software developer, it would probably be bug fixing. Working with JavaScript apps, I've used
console.log
when debugging surely more than I should have. Learning how to become efficient at finding bugs in programs is a very important skill that can save you a lot of time. Debuggers help you with that task by providing tools to analyze your programs and run control execution flow.In this post, my friend Burke Holland will walk you through the steps that can help you find issues more efficiently in Node.js apps, with short videos that run for less than 5 minutes each.
The best tool is the one you always have at hand. While it's surely not the most user-friendly or fully-featured debugger out there, Node.js comes with a built-in debugger that works straight from the command line.
It's perfect to start learning how to run your code step by step, and can also be very handy in case you're trying to debug an issue through a remote SSH session.
One excuse I've heard a lot from people not using a debugger is that it would take too much time to configure. Whether you prefer running your code from command line or directly from the UI, VS Code has your back and you can start debugging without the need to configure anything if you're in a pinch.
And when you have a few minutes to spare, you can create one or more debug configurations that can be shared with your coworkers, so there's no excuse anymore.
Once you're ready to go, let's try digging into a real issue and see how the different tools can be used to analyze the bug and fix it, using breakpoints effectively. And without adding a single
console.log
in the process.Finding the root cause of a crash is not an easy task, especially if you're exploring a large codebase. Stack traces look messy at first, but they're really valuable when you're trying to understand what's going on and can even be used to rewind the program execution to the state before an exception happened.
Now we're getting serious. After walking through all the debugging basics, it's time to have a look at more advanced features of the VS Code debugger, such as conditional breakpoints, watchers and logpoints. And I love logpoints. With them, there's no more excuse to leak a
console.log
when deploying to production.I hope that this short overview will convince you to launch your debugger more often to get used to it, and ultimately drop the
console.log
approach. Or not. But at least you have more tools in your hands to track and fix those nasty bugs.If you would like to get more into details and practice your skills on a concrete example, you can also take a look at this interactive tutorial on debugging Node.js apps. You can also try doing the fixes by yourself on the source code used in the videos here.
Thanks for reading!
Also published at https://dev.to/azure/debug-node-js-app-with-built-in-or-vs-code-debugger-41n4