How To Debug Node.js Application With Command Line

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

1. Start Node JS Application Debugger.

  1. For example, if we want to debug the js file web_server.js in the article Use Node.js To Create Http Web Server And Process Web Page Example, we need to first start the application debugger with the below command in a dos terminal.
    node debug web_server.js
  2. You can see the 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://127.0.0.1:9229/324b5664-f60c-483e-9ec4-efa68f62eb84
    Break on start in web_server.js:1
  3. 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.

  1. After you start the node application in debug mode, you can use the below debug command to debug it.
  2. setBreakpoint(10): Set a breakpoint in line 10.
  3. next: Execute to next js statement.
  4. cont: Continue to execute until the stop at the next breakpoint or application exit.
  5. step: Step into the currently executing function if the current statement is a function.
  6. out: Step out the currently executing function.
  7. backtrace: Display current executing stack trace.
  8. repl: Start node REPL ( Read Eval Print Loop ) to inspect variable value and execute code.
  9. watch(expr): Add expression to watch list, so the expression value will be displayed when code executing.
  10. 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.

  1. When you start the debugger, it will stop at the first line of the source code by default.
    C:\WorkSpace\node-js-files>node inspect web_server.js
    < Debugger listening on ws://127.0.0.1:9229/45edd127-aba9-440c-b9bb-317207b1254f
    < For help see https://nodejs.org/en/docs/inspector
    Break on start in web_server.js:1
    > 1 (function (exports, require, module, __filename, __dirname) { // Include http module.
    ons.
     2 var http_module = require("http");
     3
  2. Input the command next 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
    ons.
    > 2 var http_module = require("http");
  3. Input setBreakpoint(10) to set a breakpoint at line 10, now you can see it add a > at the beginning of line 10. Because line 10 is a code comment in our example, it will set the breakpoint at line 11. Also because the breakpoint is set in the function so only when the function is invoked the execution will stop at that breakpoint.
    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';
     9
     10 // This is the body content length.
    >11 var body_length = body_content.length;
  4. Input the cont command, the node application will execute to the end because there do not have any breakpoints in the rest code.
    debug> cont
    < Web server is running on port 8000, press Ctrl + C to exit.
  5. Now browse URL http://localhost:8000 in any web browser, you will find the result can not return immediately, the browser is hanged. This is because when browsing the above url, the code execution stops at the breakpoint we just set.
    break in web_server.js:11
     9
     10 // This is the body content length.
    >11 var body_length = body_content.length;
     12
     13 // Send header data to client.
  6. Now input the command repl to start the interactive console, you can input the 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
    undefined
    > body_content
    'Hello this web server is implemented by Node.js'
  7. 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)
    repl:1
    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)
  8. Input the command cont to execute the program until the client browser gets the result page.
0 0 votes
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x