How To Debug Node.js Application With Command Line

Node.js provide a default command line debugger for coder to debug their node JavaScript file. It is something like C++ debugger. It is not usually used today because there are a lot of GUI debugger for node application, but if you know how to use the command line debugger, it can help you greatly when you are debugging the js file in shell environment.

1. Start Node JS Application Debugger.

For example, if we want to debug the js file in article Use Node.js To Create Http Web Server And Process Web Page Example web_server.js, we need to first start the application debugger with below command in a dos terminal.

node debug web_server.js

You can see below error message if your Node.js version is 8.0 or higher.

C:\WorkSpace\node-js-files>node debug web_server.js
(node:9084) [DEP0068] DeprecationWarning: `node debug` is deprecated. Please use `node inspect` instead.
< Debugger listening on ws://

start node application with debugger

That means for node version 8.0 or higher you should use inspect to replace debug in node command parameter as below.

node inspect web_server.js

2. Node Application Debug Command.

After you start the node application in debug mode, you can use below debug command to debug it.

  1. setBreakpoint(10) : Set a break point in line 10.
  2. next : Execute to next js statement.
  3. cont : Continue to execute until stop at next break point or application exit.
  4. step : Step into current executing function if current statement is a function.
  5. out : Step out current executing function.
  6. backtrace : Display current executing stack trace.
  7. repl : Start node REPL ( Read Eval Print Loop ) to inspect variable value and execute code.
  8. watch(expr) : Add expression to watch list, so the expression value will be displayed when code executing.
  9. list(n) :List the N line in front and back of the current stop line in the debugger.

3. Debug Node Application In Command Line Example.

When you start the debugger, it will stop at the first line of code by default.

C:\WorkSpace\node-js-files>node inspect web_server.js
< Debugger listening on ws://
< For help see
Break on start in web_server.js:1
> 1 (function (exports, require, module, __filename, __dirname) { // Include http module.
 2 var http_module = require("http");

Input command next it will run to the next line of code.

debug> next
break in web_server.js:2
 1 (function (exports, require, module, __filename, __dirname) { // Include h
> 2 var http_module = require("http");

Input setBreakpoint(10) to set a break point at line 10, now you can see it add a > at the beginning of line 10. Because line 10 is code comment in our example, it will set the break point at line 11. Also because the break point is set in the function so only when the function is invoked the execution will stop at that break point.

debug> setBreakpoint(10)
 6 {
 7 // Below text will be sent back to client.
 8 var body_content = 'Hello this web server is implemented by Node.js';
 10 // This is the body content length.
>11 var body_length = body_content.length;

Input cont command, the node application will execute to the end because there do not has any break point in rest code.

debug> cont
< Web server is running on port 8000, press Ctrl + C to exit.

Now browse http://localhost:8000 in any web browser, you will find the result can not return immediately, the browser is hanged. This is because when browse above url, the code execution stop at the break point we just set.

break in web_server.js:11
 10 // This is the body content length.
>11 var body_length = body_content.length;
 13 // Send header data to client.

Now input repl command to start the interactive console, you can input variable name in the console and it will print out the variable value. Input Ctrl + C to return back to debugger console.

debug> repl
Press Ctrl + C to leave debug repl
> body_length
> body_content
'Hello this web server is implemented by Node.js'

Input  watch(body_content) command in debugger console, it will show you below error message because the body_content variable is defined in the function, it is not a global variable.

debug> watch(body_content)

ReferenceError: body_content is not defined
 at repl:1:7
 at ContextifyScript.Script.runInContext (vm.js:59:29)
 at Object.runInContext (vm.js:120:6)
 at REPLServer.controlEval [as eval] (node-inspect/lib/internal/inspect_repl.js:521:25)
 at REPLServer.onLine (repl.js:468:10)
 at emitOne (events.js:116:13)
 at REPLServer.emit (events.js:211:7)
 at REPLServer.Interface._onLine (readline.js:282:10)
 at REPLServer.Interface._line (readline.js:631:8)
 at REPLServer.Interface._ttyWrite (readline.js:911:14)

Input command cont to execute the program until client browser get the result page.

access node js http web server in web browser result

debug node application with command line example

0 0 vote
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x