Sitemap
Farhad

Farhad

Press enter or click to view image in full size

Debugger in Nodejs

3 min readApr 19, 2025

--

A command-line debugging/inspecting utility for Nodejs(Not full-featured).

Table of Contents

  • What is debugger anyway?
  • Feature and Command Reference
  • Example
  • V8 Inspector Integration (Advanced)

What is debugger anyway?

A debugger is like a pause and rewind button for your code so you can see what’s going wrong and fix it.

Usage

To use it, start Node.js with the inspect argument:

node inspect myscript.js

You’ll see something like:

Debugger listening on ws://127.0.0.1:9229/your-unique-id

The debugger automatically breaks on the first executable line. To run until the first breakpoint, use NODE_INSPECT_RESUME_ON_START=1.

NODE_INSPECT_RESUME_ON_START=1 node inspect myscript.js

Feature and Command Reference

Execution Info — Inspect call stack and source code

  • backtrace, bt – Show the current call stack
  • list(5) – Show 5 lines of source code before and after the current line

Execution Stepping — Control line-by-line execution flow

  • cont, c – Continue execution
  • next, n – Step to next line
  • step, s – Step into function
  • out, o – Step out of current function
  • pause – Pause running code (like the pause button in Developer Tools)

Execution Control — Start, restart, or stop script execution

  • run – Run the script
  • restart – Restart the script
  • kill – Stop/kill the running script

Breakpoints — Pause code and inspect what’s happening

  • setBreakpoint(), sb() – Set a breakpoint on the current line
  • setBreakpoint(10), sb(10) – Set a breakpoint on line 10
  • setBreakpoint('myFunc()') – Break at the start of function myFunc
  • setBreakpoint('script.js', 1) – Break on line 1 of script.js
  • setBreakpoint('script.js', 1, 'num < 4') – Break on line 1 only if num < 4 is true
  • clearBreakpoint('script.js', 1), cb(...) – Remove breakpoint from line 1 of script.js

Performance Profiling — Profile CPU and heap usage

  • profile – Start CPU profiling
  • profileEnd – Stop current profiling session
  • profiles – List all completed profiling sessions
  • profiles[n].save('node.cpuprofile') – Save session as JSON
  • takeHeapSnapshot('node.heapsnapshot') – Save a heap snapshot as JSON

Evaluate & Explore — Interact with and evaluate expressions

  • repl – Open interactive prompt in current script context(Ctrl + C to exit)
  • exec expr, p expr – Run and print the result of an expression

Watchers — Track and manage variable changes

  • watchers — Show all active watchers
  • watch('myVar') — Start watching a variable or expression
  • unwatch('myVar') — Stop watching a variable
  • unwatch(index) – Remove watcher by its index

Various — Miscellaneous utilities and info

  • scripts – List all loaded scripts
  • version – Display the V8 engine version
  • help - All available commands

Note: Pressing enter without typing a command will repeat the previous debugger command.

Example

File: add.js

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

console.log("2 + 3 =", add(2, "3")); // Bug: string concatenation!
node inspect add.js
debug> setBreakpoint('add.js', 2)  # Stop at function
debug> run
debug> exec a # Shows 2 (number)
debug> exec b # Shows "3" (string - bug!)
debug> kill

Advanced: V8 Inspector Integration (for Node.js)

The V8 Inspector lets you debug Node.js apps using Chrome DevTools. It communicates via the Chrome DevTools Protocol.

Basic Usage

Start your app with the --inspect flag to enable DevTools:

node --inspect index.js

You’ll see something like:

Debugger listening on ws://127.0.0.1:9229/your-unique-id

Open Chrome and go to chrome://inspect to start debugging.

Which Flag Should You Use?

  • --inspect=9222 – Same as --inspect, but on port 9222
  • --inspect – No wait for DevTools, starts the app immediately
  • --inspect-wait – After DevTools connect, starts the app
  • --inspect-brk – After DevTools connect, pauses the app (on the first line)

✅ Use --inspect-brk if you want to debug from line 1 without any execution beforehand.

Thanks for reading.

Get Farhad’s stories in your inbox

Join Medium for free to get updates from this writer.

Hope it was useful. 🙂

Some content adapted from Node.js Documentation. © Node.js contributors, used under the MIT License.

creative-commons-by-21pxcreative-commons-sa-21px

--

--