Home  >  Article  >  Web Front-end  >  Some quick and easy ways to debug Node.js programs

Some quick and easy ways to debug Node.js programs

青灯夜游
青灯夜游forward
2020-08-31 10:16:472381browse

Some quick and easy ways to debug Node.js programs

There are always various challenges when debugging a program. Node.js’s asynchronous workflow adds additional complexity to this arduous process. Although the V8 engine has made some updates to facilitate access to asynchronous stack traces, in many cases we only encounter errors on the main thread of the program, which makes debugging somewhat difficult. Likewise, when our Node.js program crashes, we often need to rely on some sophisticated CLI tool to analyze the core dump.

In this article, we will introduce some easy ways to debug Node.js programs. [Video tutorial recommendation: node js tutorial]

Logging

Of course, no development toolbox does not provide logs. We tend to place console.log statements throughout our code in local development, but this is not a truly scalable strategy in production. You may need to do some filtering and cleansing, or implement a consistent logging strategy to identify important information.

To implement a proper log-oriented debugging strategy, you can use a logging tool such as Pino or Winston. These will allow you to set the log levels (INFO, WARN, ERROR) and they will allow you to print detailed log messages locally while only Print critical log messages. You can also stream these logs to an aggregator or elsewhere, such as LogStash, Papertrail or even Slack.

Using Node Inspect and Chrome DevTools

Logging only allows us to understand why the program is not running as expected. For complex debugging, we will want to use breakpoints to examine how the code behaves as it executes.

For this, you can use Node Inspect. Node Inspect is a debugging tool that comes with Node.js. It's really just an implementation of the Chrome DevTools that lets you add breakpoints, control step-by-step execution, view variables, and follow the call stack.

There are two ways to start Node Inspect, but probably the easiest is to call your Node.js application using the --inspect-brk flag:

$ node --inspect-brk $your_script_name

Some quick and easy ways to debug Node.js programs

After launching the program, go to the chrome://inspect URL in the Chrome browser to enter Chrome DevTools. With Chrome DevTools, you have everything you need to debug JavaScript in the browser. The most useful feature is the ability to check memory. You can Get a heap snapshot and configure memory usage to understand how memory is allocated and possible patterns and memory leaks.

Use a supported IDE

Many modern IDEs not only have the ability to launch programs in some way, but also support debugging Node programs. In addition to having many of the features found in Chrome DevTools, they also have their own features, such as the ability to create log points and allowing you to create multiple debug profiles. You can get more information about these IDEs by checking out the Node.js Guide to Inspector Clients.

Using NDB

Some quick and easy ways to debug Node.js programs

Another option is to install ndb, which is a standalone debugger for Node.js, and the browser Similar to DevTools, it acts like an isolated local debugger. It also has some extra features not available in DevTools. It supports local editing, which means you can modify your code and get updated logic supported directly by the debugger platform. This is useful for quick iterations. w

Postmortem Debugging

Suppose your program crashes due to a catastrophic error (such as a memory access error). These may be rare, but they do happen, especially if your application relies on native code.

To investigate this type of problem, you can use llnode. When a program crashes, llnode can inspect JavaScript stack frames and objects by mapping them to objects on the C/C side. In order to use it, you first need a core dump of your program. To do this you need to use process.abort instead of process.exit to close the process in your code. When you use process.abort, the Node process generates a core dump file when it exits.

To better understand what llnode can provide, this video demonstrates some of its features.

Useful Node Modules

In addition to all of the above, third-party packages are recommended for further debugging.

debug

The first one is simply called debug. Using debug, you can assign specific namespaces to log messages based on function names or entire modules. Which messages are then printed to the console can be selected via specific environment variables.

For example, this is a Node.js server that is logging several messages from the entire program and middleware stack, such as sequelize, express:application and express:router:

Some quick and easy ways to debug Node.js programs

If we set the DEBUG environment variable to express:router and launch the same program, then only Showing messages tagged express:router:

Some quick and easy ways to debug Node.js programs

By filtering messages in this way, you can drill down into how individual parts of your program behave without having to Logging of significant code changes.

trace and clarify

trace and clarify are best used together.

trace Extend your async stack trace by providing more details about the asynchronous method being called, a roadmap that Node.js does not provide by default. clarify Helps by removing all information from stack traces specific to Node.js internals. This allows you to focus on function calls only to your program.

These modules are not recommended to be run in a production environment! They should only be enabled when debugging in a local development environment.

For more programming-related knowledge, please visit: Programming Teaching! !

The above is the detailed content of Some quick and easy ways to debug Node.js programs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete