CodeBlanks Logo Icon CodeBlanks

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.

Practice 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)).

Starter code

index.js
// Define the divide function
// write here

// Add a breakpoint
// write here

// Print "Ready: ..."
// write here

Sample solution

index.js
function divide(a, b) {
  if (b === 0) throw new Error("Divide by zero");
  debugger;
  return a / b;
}
console.log("Ready: " + divide(10, 2));

Expected output

Hazır: 5

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Complete the inspect flag

Question
node --____-brk dosya.js
Answer
node --inspect-brk dosya.js

Explanation: The --inspect-brk flag pauses on the first line.

2 Match each piece to its role

Term Meaning
--inspect-brk Pauses on first line
chrome://inspect Opens DevTools
9229 Default port
WebSocket Connection protocol

3 What opens the DevTools interface?

index.js
chrome://inspect
  • chrome://inspect Correct answer
  • node --version
  • npm install
  • node dosya.js

Explanation: chrome://inspect opens DevTools.

All lessons in Intermediate