0
0
Node.jsframework~5 mins

Chrome DevTools for Node.js in Node.js

Choose your learning style9 modes available
Introduction

Chrome DevTools helps you find and fix problems in your Node.js code by letting you see what your program is doing step-by-step.

When your Node.js program is not working as expected and you want to find the bug.
When you want to check how variables change while your program runs.
When you want to understand the flow of your code better by pausing and stepping through it.
When you want to measure how long parts of your code take to run.
When you want to inspect errors with more detail than just console messages.
Syntax
Node.js
node --inspect-brk your_script.js

Use --inspect-brk to start Node.js and pause on the first line for debugging.

After running this, open chrome://inspect in Chrome to connect the debugger.

Examples
Starts app.js with debugging enabled and pauses before the first line.
Node.js
node --inspect-brk app.js
Starts app.js with debugging enabled but does not pause at start.
Node.js
node --inspect app.js
Starts debugging on port 9229 (default port) for remote debugging.
Node.js
node --inspect=9229 app.js
Sample Program

This simple Node.js program adds two numbers and prints the result. You can run it with node --inspect-brk filename.js and use Chrome DevTools to step through each line, watch variables, and understand how the code runs.

Node.js
console.log('Start');

function add(a, b) {
  const result = a + b;
  return result;
}

const sum = add(5, 7);
console.log('Sum is:', sum);
OutputSuccess
Important Notes

Make sure you have Google Chrome installed to use Chrome DevTools.

Use chrome://inspect in Chrome to find and open your Node.js debugging session.

Remember to remove debugging flags when running your app in production.

Summary

Chrome DevTools lets you pause and inspect your Node.js code while it runs.

You start debugging by running Node.js with --inspect or --inspect-brk flags.

Use Chrome's chrome://inspect page to connect and debug your Node.js app visually.