Master debugging techniques in Visual Studio Code with this step-by-step guide.
Visual Studio Code (VS Code) is one of the most popular code editors, and its built-in debugging tools make it a powerful environment for identifying and fixing issues in your code. This guide will walk you through everything you need to know about debugging in VS Code, from basic setup to advanced techniques.
launch.json
file to configure debugging settings.Ctrl+Shift+D
).launch.json
file in the .vscode
folder.Example launch.json
for Node.js:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Node.js",
"program": "${workspaceFolder}/app.js",
"skipFiles": ["<node_internals>/**"]
}
]
}
F5
.F5
): Resume execution until the next breakpoint.F10
): Execute the next line of code, but don’t step into functions.F11
): Step into the function being called.Shift+F11
): Step out of the current function.Ctrl+Shift+F5
): Restart the debugging session.Shift+F5
): Stop debugging.Example:
for (let i = 0; i < 10; i++) {
console.log(i); // Breakpoint with condition: i === 5
}
Example:
console.log('Current value of i:', i); // Logs to the console
debug.inlineValues
).launch.json
and use the Add Configuration button.Example launch.json
for multi-target debugging:
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Debug Server",
"program": "${workspaceFolder}/server.js"
},
{
"type": "node",
"request": "launch",
"name": "Debug Client",
"program": "${workspaceFolder}/client.js"
}
],
"compounds": [
{
"name": "Debug Both",
"configurations": ["Debug Server", "Debug Client"]
}
]
}
python
configuration in launch.json
.{
"name": "Python: Current File",
"type": "python",
"request": "launch",
"program": "${file}",
"console": "integratedTerminal"
}
node
configuration in launch.json
.{
"type": "node",
"request": "launch",
"name": "Debug Node.js",
"program": "${workspaceFolder}/app.js"
}
cppdbg
configuration in launch.json
.{
"name": "Debug C++",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/app",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb"
}
F5
: Start debugging.F9
: Toggle breakpoint.Ctrl+Shift+D
: Open the Run and Debug view.launch.json
configuration and ensure the correct program path is specified.By mastering these debugging techniques, you’ll be able to efficiently identify and fix issues in your code, making your development process smoother and more productive. Happy debugging! 🚀