How to Debug with VS Code Debugger

Search for a command to run...

No comments yet. Be the first to comment.
Several times, I have been involved in starting a project/feature and realising that there exist issues that are yet to be discussed, either with external teams, other departments, or the relations team of a third-party provider. This has made me spe...

The SOLID principles are a set of five principles, amongst others, guiding how software is designed to be less fragile, less rigid, more testable, maintainable, and readable. It influences how we write code in such a way that future changes do not th...

GraphQL is a query language and a runtime. As a query language, unlike SQL(Structured Query Language), it never interacts with a database. It speaks with the API directly about what it needs and a server-side program that runs your queries using the ...
I recently installed Bun on my windows system. This article is a step-by-step guide on the process involved in doing so. It also includes the challenges I went through to ensure a successful installation. I will assume you have wsl on your local mach...
Disclaimer: This write-up is not written to tell you which is better or which will come out victorious. Instead, the writer seeks to help you understand what they are and the inherent differences between the two of them. The internet today is at an a...
Debugging is a part of the act of creating beauty with programming languages. It turns one into an investigator seeking out why the work of art did not turn out as expected. One way we all learned at the beginning was to output different values while seeking out the culprit. This article looks at how it is done using the VS code's debugger, javascript as the programming language, and Node.js as the runtime environment.
I will assume you have Node.Js and VS code editor installed on your system and you can create a REST API. The code used is found in this repo.
Breakpoints specify where the running code will break into the debugger. You can create a breakpoint to be absolute or conditional.
Absolute Breakpoints: The debugger stops running the code when it gets to where a breakpoint is specified. It turns on or off by clicking on the editor margin.

Conditional Breakpoints: Conditional breakpoint signifies that a criterion becomes true before the breakpoint breaks the code execution. To create a conditional breakpoint, you right-click on the editor margin and select the one you want. Two types of conditional breakpoint exist:
Expression: The breakpoint breaks the code when the expression becomes true.

Hit Count: It determines how many times a line of code needs to be hit before the code breaks execution at that line.

Logpoint: Logpoint is a breakpoint which does not break the running code but prints a message. The value in a variable can be displayed by putting the braces in curly braces. A logpoint can be created by right-clicking on the editor margin and selecting logpoint.

After creating each breakpoint it appears in the BREAKPOINTS section at the bottom of the sidebar.
This section appears at the top sidebar when a debug section is paused by a breakpoint or with the use of the pause icon on the debug toolbar. The variable section allows you to:

This section allows you to focus on the specific variable(s). You choose which variable to focus on by right-clicking on the variable and selecting the "Add to Watch" option.
The debug toolbar appears at the top of the editor when a debug section starts.
From left to right each of the icons represents:
It displays logged output while debugging a program. This output is logged either with the Node.js console module or VS code's debugger logpoint. To open the debug console, click on view and select Debug Console or use the shortcut Ctrl + Shift + Y.
We have a function in a file called app.js which finds the sum of all the numbers in an array.
function add (data) {
let sum = 0;
let counter = 0
for (let element of data) {
counter++
sum = element + sum
}
return sum
}
const result = add([1, 2, 3, 4, 5])
In other to run the debugger, we have to:
Click on the run icon in the activity bar.
Since no launch.json file has been created yet, the run start view is shown as seen above
Click on the Run and Debug button. A prompt appears asking you to select the environment.

This selection will be made based on where your code is meant to run. Since the function will be running on the Node.js environment. Node.js will be selected.

From the image above app.js was run with VS code's debugger. So it begs the question, is that all? No!
The features above show there is more to debugging with VS code's debugger.
launch.json file in a .vscode folderSelect "Node.js" as environment: This creates a launch.json file in a .vscode folder. The content of the file is:
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "${file}"
}
]
}
The program value is generated based on what file is open. The value can be edited to point to the file that starts the API server.
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"skipFiles": [
"<node_internals>/**"
],
"program": "api/index.js"
}
]
}
Click on the Start Debugging icon. It starts up the server and displays whatever message you wrote to ensure the server is running.

You can make a request with any HTTP method using tools like postman etc.
This article has explained the features of VS code debugger. How to set it up for use in debugging a function and also a web API. VS code also has debuggers for other languages and they are found under extensions by searching for debuggers. Will like to hear from you about other ways you have used the VS code's debugger in the comment section below. Thanks.