Node.js
Chrome DevTools
Learn to debug Node.js with Chrome DevTools.
Debugging with Chrome DevTools
If you want a visual interface instead of the terminal, use Chrome DevTools. The command node --inspect-brk file.js starts Node.js as a debugging server. Then open chrome://inspect in Chrome and click the inspect link.
The --inspect-brk flag pauses code on the first line, so you catch early-running code. With only --inspect the code runs normally and the debugger does not pause until attached. In DevTools you set breakpoints by clicking the gutter next to a line number. Variables appear live in the panel; hover to expand their values. This approach suits inspecting complex objects visually.
Remote connection
DevTools connects to the Node.js process over WebSocket. The default port is 9229. If you run multiple processes, choose a different port with --inspect-brk=9230.
To debug Node.js on a remote server, write --inspect=0.0.0.0:9229; this allows connections from outside (only on trusted networks). In Chrome's chrome://inspect page, click Configure... to add the target server. Once connected, all local debugging features (breakpoints, variable inspection, call stack) work remotely too. This is useful for debugging an error on a production server from your local machine.
Exercise Write a script for DevTools
Define function divide(a, b) { if (b === 0) throw new Error("Divide by zero"); return a / b; }. Add a debugger; line. Print with console.log("Ready: " + divide(10, 2)).
Show example solution
function divide(a, b) {
if (b === 0) throw new Error("Divide by zero");
debugger;
return a / b;
}
console.log("Ready: " + divide(10, 2));
Test yourself
-
Complete the inspect flag
node --____-brk dosya.js
Show answer
node --inspect-brk dosya.js
-
Match each piece to its role
- --inspect-brk
- Pauses on first line
- chrome://inspect
- Opens DevTools
- 9229
- Default port
- WebSocket
- Connection protocol
-
What opens the DevTools interface?
chrome://inspect
- chrome://inspect
- node --version
- npm install
- node dosya.js
Show answer
chrome://inspect — chrome://inspect opens DevTools.
Next lesson: console methods