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
- Install Visual Studio Code.
- Install Node.js, which runs the JavaScript example.
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
-
Create an empty folder named
debugging-practiceand 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.
-
In the Explorer view (⇧⌘E (Windows, Linux Ctrl+Shift+E)), create a file named
app.js. -
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
-
Select Terminal > New Terminal. The terminal opens in your project folder.
-
Run the program:
node app.jsThe 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.
-
Return to the
app.jseditor and select the margin to the left of the line number besideconst total = price + quantity;on line 2. A red circle marks the breakpoint.Alternatively, place the cursor on that line and press F9.
-
Select Run > Start Debugging (F5). If prompted to choose a debugger, select Node.js.
-
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
-
In the Debug toolbar, select Step Over (F10) once.
The debugger runs the calculation and pauses on
return total;. Hover overtotalin the editor or find it under VARIABLES. Its value is13. -
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. -
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
-
Keep the breakpoint on line 2 and start debugging again with F5.
-
When execution pauses, select Step Over once. Confirm that
totalis now30under VARIABLES. -
Select Continue to finish the program.
-
In the terminal, run
node app.jsagain. 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 --versionworks 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.jsis the active editor, the breakpoint is on line 2, and you used Start Debugging, not Run Without Debugging ornode app.jsin a regular terminal. - The breakpoint is hollow or unavailable: hover over it to read the debugger's message. Save
app.jsand confirm that you selected Node.js. - The Debug Console can't find a variable: pause inside
calculateTotaland select that function's frame in CALL STACK. After the program exits, start debugging again before inspecting its variables.
Next steps
- Debug your own code with the runtime and debugger for your language.
- Explore debugging tools, including watches, the call stack, and conditional breakpoints.
- Create a reusable debug configuration when your application needs arguments, environment variables, or a different entry point.