Debugging is as important as development. If you can’t debug, you wont be able to develop big applications. Node uses V8 Runtime JavaScript engine, which comes with an extensive debugger. This debugger is accessible out of process via a TCP-Protocol. Node has a built-in client which uses this debugger. Today we are going to see how we can use this debugger to debug a node program.

Here is the program which we are gonna debug

//Declaring few variables
var a=1;
var b=2;
var c;

//addition via a function
c = addition(a,b);

console.log(a+" + "+b+" = "+c);

function addition (a,b) {
	return a+b;

Start/Stop debugging: run, kill or restart app

Start debugging

Generally you run a program with the command: “node MyAppFile.js”. To run the application in debugging mode the command is:

node debug MyAppFile.js

Executing the command mentioned above, will lead you to the debugging panel as well as it will start the debugging for “MyAppFile.js”.
node debug command

Stop debugging the app

To stop or kill the app you debugging, the command is:


This command will stop running the app but will not bring you out from the debugging panel.

Run the app

To run the app again, you simply can use the following command:


This command will simply start running the app from the beginning.

Restart the running app

At any given point of time you can restart the app. To do that use the following command:


Come out of the debug panel

Use ctrl+c two times simultaneously to come out of the debug panel.

Setting breakpoint

There are two ways to put a breakpoint in a program. Predefined or at runtime.

Way 1:

This is similar to setting a breakpoint in Visual Studio, or any other IDE like that. Before running the app, you open the MyAppFile.js file in a text editor. Now write “debugger;” at any part of the program and it will be the breakpoint. Below is an example:

var a=1;
var b=2;
var c;
//addition via a function
c = addition(a,b);

Way 2:

In this way you can set a breakpoint while running the app, without doing any modification in the code. You do it using the following command:

setBreakpoint('MyAppFile.js', 23);
//clearBreakpoint() does exactly the opposite
clearBreakpoint('MyAppFile.js', 23);

The function is taking the first argument as file name, cause if your app may require some other files.
node setBreakpoint

Stepping in Node debugger

Here we are describing the stepping commands comparing with that the developer’s tools of Google Chrome.

Continue execution: F8 in Chrome

In Chrome developer’s tool you can continue execution of the program, or go to one breakpoint to next by pressing “F8″ key. In Node you do it with the following command:


The “c” command can also do the same for you.

Step next: F10 in Chrome

In case of Chrome you use “F10″ to go to the next stem. Similar thing is done by the following command in Node:


The “n” command can also do the same for you.

Step Into: F11 in Chrome

To go inside a function or doing similar thing you use “F11″ in Chrome. Same thing is done in Node by executing the following command:


The command “s” can perform the same thing for you.

Step Out: Shift + F11 in Chrome

To go out of the step mentioned in the previous point you use “Shift + F11″ in Chrome. Same thing is done in Node by executing the following command:


The command “o” can perform the same thing for you.

pause: pause of Chrome

The functionality of the pause button in Chrome developer’s tool can be achieved by the following command in Node:


Checking the values, watch expressions, executing scripts

Node debugger is not as smart as that in Chrome. Still it provides some simple ways to retrieve info from the variables or the running program.

repl: check expression values, execute script

To open debugger’s repl for evaluation in debugging script’s context, you can use the following command:


Once you are in debugger’s repl, you can type any variable name and hit enter to get the value. You can execute expressions also.
You can press ctrl+c to come out of repl.
node repl

Watchers: Watch expressions in Chrome

As you can set watch expressions in Node also as you do it in Chrome. Below is the example of adding an expression in watcher, removing it, and getting the values of all the expressions added in watchers.

//adding variable b and c in the watch expression
//removing b from watch expression array

At any point of time you can add any expression in the watch array or remove one.
To view the values of the watch expressions, use the following commands:
Even after every command of step forward, watchers will be automatically printed.

Debug a Node program with REST service

This is exactly similar as the points mentioned above. Here the breakpoints inside the route callback will not be hit when you start debugging; rather it will be when you hit the required url. Lets take the example of the app below, which is similar to our first node application:

// Load the http module to create an http server.
var http = require('http');
// Load the node-router library by creationix
var server = require('express')();
// Configure our HTTP server to respond with Hello World the root request
server.get("/", function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("Hello World");
server.get("/test", function (request, response) {
response.writeHead(200, {"Content-Type": "text/plain"});
response.write("We are in test route \n");
// Listen on port 8080 on localhost
server.listen(8000, "localhost");

Here I’ve set a debugger in “/test” route callback. Now lets debug with the steps below:

  1. Open your terminal or command prompt and navigate to your project folder.
  2. Debug with the command: “node debug Index.js”
  3. Execute next with the command: “cont”
  4. Now hit the url “http://localhost:8000/test” in your browser.
  5. Now in your terminal or command prompt you will find the breakpoint is hit.

All debugging commands together

Execution controlls:

  1. run: run the program.
  2. kill: stop the running program.
  3. pause: same as pause in Chrome debugger’s tool.
  4. restart: restart the running script at any point of time.

Breakpoint controlls:

  1. setBreakpoint(), sb() – Set breakpoint on current line
  2. setBreakpoint(line), sb(line) – Set breakpoint on specific line
  3. setBreakpoint(‘fn()’), sb(‘fn()’) – Set breakpoint on a first statement in functions body
  4. setBreakpoint(‘script.js’, 1), sb(‘script.js’, 1) – Set breakpoint on first line of script.js
  5. clearBreakpoint, cb(…) – Clear breakpoint


  1. cont, c – Continue execution
  2. next, n – Step next
  3. step, s – Step into
  4. out, o – Step out


  1. backtrace, bt – Print backtrace of current execution frame
  2. list(5) – List scripts source code with 5 line context (5 lines before and after)
  3. watch(expr) – Add expression to watch list
  4. unwatch(expr) – Remove expression from watch list
  5. watchers – List all watchers and their values (automatically listed on each breakpoint)
  6. repl – Open debugger’s repl for evaluation in debugging script’s context


  1. scripts – List all loaded scripts
  2. version – Display v8′s version