In versions prior to Node.js v6.3, the debugger used the old V8 Debugger Protocol, which can be done through node --debug app.js. In order to perform visual breakpoint debugging in a graphical interface, you need to use editors and plug-ins such as VSCode and WebStorm, or install additional tools such as node-inspector. For most front-end developers, the DevTools that comes with the Chrome browser are no strangers. The new version of Node.js begins to support the new Chrome DevTools Protocol. You can start the new version of the debugging protocol through node --inspect app.js, and with the help of Chrome DevTools, you can achieve the same experience as debugging web pages in the past. Some editors also support the new Node.js debugging protocol, or support it by installing the Chrome plugin NIM. But if you have Node.js 6.3+ and Chrome 55+ installed on your computer, you no longer need any additional tools to easily debug your Node.js program. Breakpoint debugging First, let's create a new program file app.js to experience the new debugging method:
Then execute the following command to start the program and enter debugging mode:
You can see the following information printed out on the console:
At this point, open the Chrome browser (Chrome 55 or higher is required), enter chrome://inspect in the address bar and press Enter, the following interface will open: Click the Open dedicated DevTools for Node link on the interface to open the debugging window: In this window, click the Sources tab and find the source code file app.js under file:// in the left sidebar to see its source code. If you want to perform breakpoint debugging, just click on the line number of the source code: In the right sidebar of the window, you can see information such as Watch, Call Stack, Scope, etc. You can also modify the code directly in the source code window and press ⌘ + S (Ctrl + S on Windows) to save and it will take effect immediately. Performance Analysis The Profiler tab in the debugging interface can be used to analyze which Node.js programs have a high CPU usage: We can use the wrk command to perform a simple HTTP interface test. First, click the Start button in the interface to start recording CPU usage information, and then execute the following command in the command line window (start 5 threads, 100 concurrent connections, and last for 1 minute):
Wait for 1 minute. After the command is executed, click the Stop button in the DevTools interface and you will see the following results: In addition, you can also use the Memory tag to perform memory usage analysis. Debugging TypeScript Programs If your program is written in TypeScript, you can use ts-node to start debugging mode:
or:
Debugging a running Node.js process If the program is started without the --inspect option, you can send a SIGUSR1 signal to the process to put it into debug mode (92801 is the PID of the Node.js process being debugged, replace it according to the actual situation):
You can see that the process console prints the following prompts:
At this point, you can use Chrome DevTools to debug according to the above method. |
<<: After Android and iOS, the third largest mobile system reappears
Whenever we are pleased with the copywriting work...
If you want to become popular by creating videos ...
Many people like to trim their nose hairs, becaus...
Steam's new overlord: PlayerUnknown's Bat...
Recently, a Mr. Xia from Huangshi, Hubei, posted ...
[51CTO.com original article] As an ordinary perso...
This article will explain what actions need to be...
Do you need a membership system? The membership s...
Dingxi designated driver mini program investment ...
I always see some companies describe their job re...
The receding hairline, the piles of hair that I p...
In June this year, the "China Cardiovascular...
Source code introduction Source code screenshot v...
This article uses a case study to introduce how t...
With its dynamic design, Volkswagen CC has always...