Node.js Debugger


Node.js includes a full-featured out-of-process debugging utility accessible via a simple TCP-based protocol and built-in debugging client. To use it, start Node.js with the debug argument followed by the path to the script to debug; a prompt will be displayed indicating successful launch of the debugger:

Node.js's debugger client is not a full-featured debugger, but simple step and inspection are possible. Inserting the statement debugger; into the source code of a script will enable a breakpoint at that position in the code:

loop-debug.js
for(var i=0;i<=10;i++){
	debugger;
	console.log("Hellow World"+i);
}

run above js with below command to see values in debug mode
node debug loop-debug.js

output
  1 for(var i=0;i<=10;i++){
> 2 debugger;
  3 console.log("Hellow World"+i);
  4 }
debug> c
< Hellow World0
break in F:\mongodb\project\debug.js
  1 for(var i=0;i<=10;i++){
> 2 debugger;
  3 console.log("Hellow World"+i);
  4 }
debug> c
< Hellow World1
break in F:\mongodb\project\debug.js
  1 for(var i=0;i<=10;i++){
> 2 debugger;
  3 console.log("Hellow World"+i);
  4 }
debug> c
< Hellow World2
break in F:\mongodb\project\debug.js
  1 for(var i=0;i<=10;i++){
> 2 debugger;
  3 console.log("Hellow World"+i);
  4 }

Watchers

It is possible to watch expression and variable values while debugging. On every breakpoint, each expression from the watchers list will be evaluated in the current context and displayed immediately before the breakpoint's source code listing.

To begin watching an expression, type watch('my_expression'). The command watchers will print the active watchers. To remove a watcher, type unwatch('my_expression').

Stepping

  • cont, c - Continue execution
  • next, n - Step next
  • step, s - Step in
  • out, o - Step out
  • pause - Pause running code (like pause button in Developer Tools)

Breakpoints

  • setBreakpoint(), sb() - Set breakpoint on current line
  • setBreakpoint(line), sb(line) - Set breakpoint on specific line
  • setBreakpoint('fn()'), sb(...) - Set breakpoint on a first statement in functions body
  • setBreakpoint('script.js', 1), sb(...) - Set breakpoint on first line of script.js
  • clearBreakpoint('script.js', 1), cb(...) - Clear breakpoint in script.js on line 1