How To Effectively Debug Your Node/Express App without 100 Console Logs Using VS Code

The problem with debugging a Node/Express app by relying on tons of console logs is that it quickly becomes confusing, especially when dealing with asynchronous logic.

Maybe the issue is on line 420. You add a console log. Rats! The massive object you logged is totally useless in your investigation.

You hammer the endpoint with more and more requests, add more console logs. Great now you’ve hit a rate limit from a third party API you were experimenting with.

There’s a better, more effective way to walk through and debug your janky code. (Grab a node/express starter project here to try this out on your own)

Step 1: Add a Configuration

Open your NodeJS project in VS Code and run the project from the integrated terminal (this is important or it will not work). Now click on that bug icon on the left side of the screen.n.

No alt text provided for this image

Click on create a launch.json file

No alt text provided for this image

Select Node.js … duh.

No alt text provided for this image

Let’s Add Configuration and pick Node.js: Attach to Process

No alt text provided for this image

This configuration will allow you to pick from a list of processes and choose which one to debug. There are other configurations you can experiment with but I’ve found this option to be the most straightforward.

Step 2: Run the Project in Debug Mode

Now we’re finally ready to debug that pesky endpoint!

Start your project from the integrated terminal.

No alt text provided for this image

Click the green play icon at the top of the editor while in debug mode (by pressing that bug icon we clicked in the first step). A list of processes will be displayed which you can choose from.

No alt text provided for this image

Click on the correct process — in my case it is the top option — and we can set some breakpoints.

Step 3: Set Breakpoint(s) and Investigate!

I’ve set a breakpoint on line 212 of my controller by clicking the line number on the left hand side of the screen.

This is a perfect opportunity to use breakpoints as I have a rate limit under the free tier of an API and I don’t want to waste my limit with dozens of calls for debugging.

No alt text provided for this image

Hovering over different variables will let me know their value at the time of execution. The navigation controls at the top of the screen will allow me to step through the code line by line to see where my issue resides.

After using this method to debug my NodeJS endpoints I rarely use console logs. Breakpoints are a documented feature but it’s surprising how many people are unaware of them. Hopefully they save you a day of hair pulling trying to solve that issue on line 163 😉.

There are a couple of ways I can help you accelerate your career as a developer, land the next job or get really good at your current one:

  1. Group coaching/interview prep 10 week program you can sign up for here THIS is the last week to sign up!
  2. (JUST RELEASED) Tired of endless tutorials? Get your hands dirty with challenges guaranteed to level you up as an engineer
  3. Schedule a 15 min brainstorm session for advice on your career or interview strategy