This article reprinted from: the into translation the translator: loveky links: www.zcfy.cc/article/729: the original blog.risingstack.com/node-hero-n…

This article is the tenth in a series of tutorials called Node Heroes — in which you’ll learn how to use Node.js and develop software products with it.

In this article, you’ll learn how to debug Node.js applications using the Debug module, the built-in Node debugger, and Chrome’s developer tools.

Upcoming and past chapters:

Vulnerabilities, debugging

The terms bugs and debugging have been the jargon of engineering for decades. The first written reference to the concept of vulnerabilities comes in the following paragraph:

It has been just so in all of my inventions. The first step is an intuition, and comes with a burst, Then difficulties arise — this thing gives out and [it is] then that “Bugs” — as such little faults and difficulties are Called — show themselves and months of intense watching, study and labor are requisite before commercial success or failure is certainly reached.

Thomas Edison

Extensive use of console.log statements is the most common way to debug Node.js applications.

“Console.log is good enough for debugging small code snippets, but we recommend you use a better alternative!” @RisingStack #nodejs

Let’s take a look!

The debug module

The Debug module is one of many modules that are most often required in a project. Using this module, you can have a third party module print logs to the standard output stdout. To find out if a module uses it, you can look at the dependency declaration section of that module’s package.json file.

To use the Debug module, you need to set the Debug environment variable when launching your application. You can use the wildcard * to match names. The following code prints all express related logs to standard output.

DEBUG=express* node app.jsCopy the code

The output should look something like this:

Node.js built-in debugger

Node.js includes a full-featured out-of-process debugging tool that can be accessed via TCP or a built-in debugging client.

To launch the built-in debugger you need to launch the app like this:

node debug app.jsCopy the code

After you launch the app, you should see something like the following:

Basic usage of the Node debugger

To view information in this interface, you can use the following commands:

  • C => Continue executing the code

  • N => Execute the current line and go to the next line

  • S => jumps into the function

  • O => Executes the function and jumps to the outer code

  • repl=> Executes the code in the context of the current code

You can add breakpoints by adding debugger statements to your code.

function add (a, b) {
  debugger
  return a + b
}

var res = add('apple', 4)Copy the code
viewer

You can observe the values of expressions or variables during debugging. At each breakpoint, the expression in each observer list is evaluated in the current context and its result is printed above the code corresponding to the current breakpoint.

To use an observer, you need to declare all the expressions you want to observe. You can do this as follows:

watch('expression')Copy the code

Type Watchers to get a list of all active watchers. To stop watching an expression you need to unwatch(‘expression’).

Tip: You can switch a running Node.js process into debug mode by sending it SIGUSR1 signals. You can then connect to the debugger via Node debug -p.

“You can do this by sending it to a running Node.js processSIGUSR1Signal to switch it into debug mode. via @RisingStack

To see all the features of the built-in debugger, refer to the official documentation:Nodejs.org/api/debugge…

Debugger for Chrome

Visual tools can be helpful when you start debugging complex applications. Wouldn’t it be great to use the Chrome developer tools you’re familiar with to debug Node.js apps?

The good news is that the Chrome debug protocol has been ported into node.js modules and can be used to debug Node.js applications.

To use it, you’ll need to install node-Inspector:

npm install -g node-inspectorCopy the code

Once installed, you can start debugging by launching it like this:

node-debug index.js --debug-brkCopy the code

(--debug-brkThe argument enters a breakpoint at the first line of the program.

This opens the Chrome Developer Tool, which you can use to debug your Node.js application.

The following

Debugging isn’t really that complicated, is it?

In the next chapter of Node Hero, you’ll learn how to make your Node.js applications more secure.

If you have any questions or suggestions, please comment in the comments section below.