DEV Community

Cover image for ๐Ÿง  Mastering VS Code Debugger for Node.js: A Real-World Guide to Fixing Production Bugs Faster ๐Ÿš€
Darshan Raval
Darshan Raval

Posted on

๐Ÿง  Mastering VS Code Debugger for Node.js: A Real-World Guide to Fixing Production Bugs Faster ๐Ÿš€

Letโ€™s understand how we can debug in Advanced Mode.

๐Ÿ”Ž 1๏ธโƒฃ Variables

๐Ÿ‘‰ What is it?

When the debugger stops at a line (breakpoint), it shows the current value of all variables at that exact moment.

๐Ÿ‘‰ Example:

function add(a, b) {
  const sum = a + b;
  return sum;
}

add(5, 3);
Enter fullscreen mode Exit fullscreen mode

If you put a breakpoint on: const sum = a + b; ( Line 2 )

VS Code debugger paused at breakpoint on const sum = a + b showing red breakpoint indicator

In the Variables panel, youโ€™ll see:

VS Code Variables panel displaying values of a=5, b=3, and sum=8 during debugging

Expanded view of Variables panel in VS Code showing local scope and variable inspection

๐Ÿ‘‰ What is it used for?

  • To check if values are as expected
  • To inspect object structure
  • To examine API responses

In production debugging, this is a lifesaver.


2๏ธโƒฃ Watch

๐Ÿ‘‰ What is it?

In Watch, you manually add an expression, and the debugger shows its value at every step.

Now, let's take same example and let's put express there and check what it'll give output,

In this -> I added one expression that is "a+b" and Enter so output would be this,

VS Code Watch panel evaluating expression a+b and displaying calculated result

๐Ÿ‘‰ When to use it?

  • When you want to track a specific variable
  • When you need to repeatedly check a condition
  • When inspecting deep properties inside complex objects

Another Example is,

I want to get the length of one array so just put "a.length" and we will get output,

VS Code Watch panel showing array length evaluation using a.length expression


๐Ÿ“š 3๏ธโƒฃ Call Stack

๐Ÿ‘‰ What is it?

It shows the function call chain.

VS Code Call Stack panel showing function chain validatePayment โ†’ processOrder โ†’ main

๐Ÿ” What This Means

The stack shows the execution path:

  • main() started everything
  • main() called processOrder()
  • processOrder() called validatePayment()
  • Error occurred inside validatePayment()

This is the exact function chain.

๐ŸŽฏ Why This Is Powerful

Imagine in production:

  • You only see: Payment validation failed
  • You donโ€™t know which API triggered it
  • You donโ€™t know which middleware called it

Call stack tells you:

  • ๐Ÿ‘‰ Where the error started
  • ๐Ÿ‘‰ Who called it
  • ๐Ÿ‘‰ How execution reached there

No guessing. No extra logs.


๐Ÿ“œ 4๏ธโƒฃ Loaded Scripts

๐Ÿ‘‰ What is it?

It shows which JavaScript files are currently loaded in the debugger.

In a Node app, this includes:

  • Your source files
  • node_modules files
  • Compiled/transpiled files

๐Ÿ‘‰ What is it used for?

  • To check if the correct file is loaded
  • To verify the correct build version is running
  • To debug source map issues

VS Code Loaded Scripts panel displaying index.js, math.js, node_modules and internal Node files

You will see:

  • index.js
  • math.js
  • Node internal files
  • Files from node_modules (if any installed)

๐Ÿ” What This Means

Loaded Scripts shows all JavaScript files currently active in memory during debugging.

In a real Node app, this includes:

  • โœ… Your source files
  • โœ… node_modules packages (Express, Redis, etc.)
  • โœ… Internal Node.js files
  • โœ… Compiled/transpiled files (if using TypeScript or Babel)

๐ŸŽฏ Why This Is Useful

1๏ธโƒฃ Check if the Correct File Is Loaded
2๏ธโƒฃ Verify Correct Build Version
3๏ธโƒฃ Debug Source Map Issues (TypeScript Case)


๐Ÿ›‘ 5๏ธโƒฃ Breakpoints

Let's create one file call test.js

function getUser(id) {
  console.log("Fetching user...");

  const users = [
    { id: 1, name: "Darshan", role: "ADMIN" },
    { id: 2, name: "Rahul", role: "USER" },
  ];

  const user = users.find((u) => u.id === id);

  if (!user) {
    throw new Error("User not found");
  }

  return user;
}

function resolver(args, context) {
  console.log("Inside resolver...");

  const user = getUser(args.id);

  if (context.role !== "ADMIN") {
    throw new Error("Unauthorized access");
  }

  return user;
}

function main() {
  const args = { id: 1 };
  const context = { role: "USER" }; // Change to ADMIN later

  const result = resolver(args, context);

  console.log("Result:", result);
}

main();
Enter fullscreen mode Exit fullscreen mode

๐Ÿ”ด 1๏ธโƒฃ Normal Breakpoint

Now, put the debug point at here,

const user = getUser(args.id);
Enter fullscreen mode Exit fullscreen mode

And run the code in Javascript Debug Terminal and once debug point reach to that place then you will see,

VS Code debugger paused at resolver function breakpoint showing args and context variables

๐Ÿ‘‰ Execution will stop there.
Now check:

Variables Panel:

  • args
  • context

You can inspect:

args = { id: 1 }
context = { role: "USER" }
Enter fullscreen mode Exit fullscreen mode

Press โ–ถ Continue โ€” it will move forward.

๐ŸŸก 2๏ธโƒฃ Conditional Breakpoint

Now put breakpoint on:

const user = users.find((u) => u.id === id);
Enter fullscreen mode Exit fullscreen mode

Right-click โ†’ Add Condition:

VS Code conditional breakpoint settings dialog with id === 2 condition

Conditional breakpoint configured in VS Code to trigger only when id equals 2

id === 2
Enter fullscreen mode Exit fullscreen mode

Now change in main():

const args = { id: 2 };
Enter fullscreen mode Exit fullscreen mode

Debugger will stop only when id = 2.

Debugger paused after conditional breakpoint triggered for id=2 in VS Code

This is powerful in loops or bulk processing.


๐ŸŽฎ Debug Controls (Test Them Now)

โญ Step Over

Moves to next line
(Does NOT enter function)

โฌ Step Into

Goes inside function

If paused at:

const user = getUser(args.id);
Enter fullscreen mode Exit fullscreen mode

Press Step Into โ†’ you enter getUser.

โซ Step Out

If you're inside getUser, press Step Out โ†’ it exits and returns to resolver.

โ–ถ Continue

Runs until next breakpoint or error.

Thanks :)

Top comments (3)

Collapse
 
martijn_assie_12a2d3b1833 profile image
Martijn Assie

Wow!! This is such a hands-on guide, I love it ๐Ÿ˜ Tip: adding a tiny โ€œreal bug scenarioโ€ screenshot where the conditional breakpoint actually saved the day would make it even more epicโ€ฆ readers will see the magic in action!!

Collapse
 
darshanraval profile image
Darshan Raval

thank you so much Martijn! ๐Ÿ™Œ
That really means a lot ๐Ÿ˜Š

Collapse
 
darshanraval profile image
Darshan Raval

Happy Debugging ๐Ÿš€