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.jsYou’ll see something like:
Debugger listening on ws://127.0.0.1:9229/your-unique-idThe 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.jsFeature and Command Reference
Execution Info — Inspect call stack and source code
backtrace,bt– Show the current call stacklist(5)– Show 5 lines of source code before and after the current line
Execution Stepping — Control line-by-line execution flow
cont,c– Continue executionnext,n– Step to next linestep,s– Step into functionout,o– Step out of current functionpause– Pause running code (like the pause button in Developer Tools)
Execution Control — Start, restart, or stop script execution
run– Run the scriptrestart– Restart the scriptkill– Stop/kill the running script
Breakpoints — Pause code and inspect what’s happening
setBreakpoint(),sb()– Set a breakpoint on the current linesetBreakpoint(10),sb(10)– Set a breakpoint on line 10setBreakpoint('myFunc()')– Break at the start of functionmyFuncsetBreakpoint('script.js', 1)– Break on line 1 ofscript.jssetBreakpoint('script.js', 1, 'num < 4')– Break on line 1 only ifnum < 4is trueclearBreakpoint('script.js', 1),cb(...)– Remove breakpoint from line 1 ofscript.js
Performance Profiling — Profile CPU and heap usage
profile– Start CPU profilingprofileEnd– Stop current profiling sessionprofiles– List all completed profiling sessionsprofiles[n].save('node.cpuprofile')– Save session as JSONtakeHeapSnapshot('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 watcherswatch('myVar')— Start watching a variable or expressionunwatch('myVar')— Stop watching a variableunwatch(index)– Remove watcher by its index
Various — Miscellaneous utilities and info
scripts– List all loaded scriptsversion– Display the V8 engine versionhelp- 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.jsdebug> setBreakpoint('add.js', 2) # Stop at function
debug> run
debug> exec a # Shows 2 (number)
debug> exec b # Shows "3" (string - bug!)
debug> killAdvanced: 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.jsYou’ll see something like:
Debugger listening on ws://127.0.0.1:9229/your-unique-idOpen 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-brkif 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.







