Quickstart: debug your first application

Find out why a small program produces the wrong result by pausing it, inspecting its variables, and stepping through the code in Visual Studio Code. Then fix the bug and verify the result.

This quickstart uses a single JavaScript file and the built-in Node.js debugger. You don't need an existing project, additional extensions, packages, or a launch.json configuration.

If you already have an application to debug, see start a debugging session and choose the setup for your language.

Prerequisites

In VS Code, select Terminal > New Terminal and check that Node.js is available:

node --version

You should see a version number. If the command isn't found, restart VS Code after installing Node.js, then open a new terminal and try again.

1. Create a practice project

  1. Create an empty folder named debugging-practice and open it with File > Open Folder.

    If prompted, trust the folder you created. Only trust other projects when you know their source. Learn about Workspace Trust.

  2. In the Explorer view (⇧⌘E (Windows, Linux Ctrl+Shift+E)), create a file named app.js.

  3. Add the following code and save the file (⌘S (Windows, Linux Ctrl+S)):

    function calculateTotal(price, quantity) {
      const total = price + quantity;
      return total;
    }
    
    const price = 10;
    const quantity = 3;
    const total = calculateTotal(price, quantity);
    console.log(`Total: ${total}`);
    

The program should calculate the cost of three items priced at 10 each. The expected total is 30, but the calculation contains a bug.

2. Reproduce the problem

  1. Select Terminal > New Terminal. The terminal opens in your project folder.

  2. Run the program:

    node app.js
    

    The output is:

    Total: 13
    

Running the program shows the incorrect result. Next, use the debugger to inspect how the program calculates it.

3. Pause at a breakpoint

A breakpoint pauses the program before a line runs, so you can examine its state.

  1. Return to the app.js editor and select the margin to the left of the line number beside const total = price + quantity; on line 2. A red circle marks the breakpoint.

    Alternatively, place the cursor on that line and press F9.

  2. Select Run > Start Debugging (F5). If prompted to choose a debugger, select Node.js.

  3. Open the Run and Debug view (⇧⌘D (Windows, Linux Ctrl+Shift+D)) if it isn't already visible.

Execution pauses on line 2. The current-line highlight indicates the line that runs next, not a line that has already run. Under VARIABLES, expand Local to find price with a value of 10 and quantity with a value of 3.

4. Inspect the calculation

  1. In the Debug toolbar, select Step Over (F10) once.

    The debugger runs the calculation and pauses on return total;. Hover over total in the editor or find it under VARIABLES. Its value is 13.

  2. Select View > Debug Console. At the input prompt, enter the following expression and press Enter:

    price * quantity;
    

    The result is 30. The inputs are correct, but the code adds them instead of multiplying them. Evaluating this expression doesn't change the source file.

  3. Select Continue (F5) in the Debug toolbar to finish the program.

    The debugging session ends. Inspect variables and evaluate expressions while paused, before continuing to the end of the program.

5. Fix the bug

In app.js, replace the addition on line 2 with multiplication, and save the file:

const total = price * quantity;

6. Verify the fix

  1. Keep the breakpoint on line 2 and start debugging again with F5.

  2. When execution pauses, select Step Over once. Confirm that total is now 30 under VARIABLES.

  3. Select Continue to finish the program.

  4. In the terminal, run node app.js again. Confirm that the output is:

    Total: 30
    

You reproduced the problem, inspected the calculation, identified its cause, and verified the fix. Select the red breakpoint circle to remove it when you're finished.

Troubleshooting

  • Node.js isn't found: confirm that node --version works in a new terminal in VS Code. The built-in debugger doesn't install the Node.js runtime.
  • The program runs without pausing: confirm that app.js is the active editor, the breakpoint is on line 2, and you used Start Debugging, not Run Without Debugging or node app.js in a regular terminal.
  • The breakpoint is hollow or unavailable: hover over it to read the debugger's message. Save app.js and confirm that you selected Node.js.
  • The Debug Console can't find a variable: pause inside calculateTotal and select that function's frame in CALL STACK. After the program exits, start debugging again before inspecting its variables.

Next steps