Loading lesson path
Concept visual
Start from A
Effective debugging is a critical skill for Node.js developers.
While console.log()is useful for basic debugging, advanced techniques allow you to diagnose complex issues like memory leaks, performance bottlenecks, and race conditions. This tutorial covers advanced debugging techniques and tools to help you solve challenging problems in your Node.js applications. Advanced debugging tools provide capabilities like:
Node.js includes built-in support for the Chrome DevTools debugging protocol, allowing you to use the powerful Chrome DevTools interface to debug your Node.js applications. Starting Node.js in Debug Mode There are several ways to start your application in debug mode: Standard Debug Mode node --inspect app.js This starts your app normally but enables the inspector on port 9229.
Formula
Break on Start node -- inspect - brk app.jsThis pauses execution at the first line of code, allowing you to set up breakpoints before execution begins.
Formula
Custom Port node -- inspect = 127.0.0.1:9222 app.jsThis uses a custom port for the inspector.
After starting your Node.js application with the inspect flag, you can connect to it in several ways:
Open Chrome and navigate to chrome://inspect. You should see your Node.js application listed under "Remote Target." Click "inspect" to open DevTools connected to your application:
Formula
(usually something like devtools://devtools/bundled/js_app.html?experiments = true&v8only = true&ws = 127.0.0.1:9229/...).
Once connected, you can use the full power of Chrome DevTools:
Set breakpoints, step through code, and watch variables
View the current execution stack, including async call chains
Inspect local and global variables at each breakpoint
Use the Sources panel's "Pause on caught exceptions" feature (the pause button with curved lines) to automatically break when an error occurs.
Formula
Visual Studio Code provides excellent built - in debugging capabilities for Node.js applications.Setting Up Node.js Debugging in VS Code You can start debugging your Node.js application in VS Code in several ways: launch.json Configuration:
Formula
Create a.vscode/launch.json file to define how VS Code should launch or attach to your application.Formula
Enable auto - attach in VS Code settings to automatically debug any Node.js process started with the--inspect flag.
Use the JavaScript Debug Terminal in VS Code to automatically debug any Node.js process started from that terminal.
Example launch.json Configuration
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/app.js",
"skipFiles": ["<node_internals>/**"]
},
{
"type": "node",
"request": "attach",
"name": "Attach to Process",
"port": 9229
}]
}VS Code provides powerful debugging capabilities: