As the saying goes, “if you want to do a good job, you must first sharpen your tools”. Debugging is a problem that every developer will encounter, and it is particularly important to choose a suitable debugging tool. In addition to console.log, this section introduces a debugging tool that combines Node.js with Chrome Devtools to enable you to debug Node.js applications using your browser.
Start the debugger
Create test code
const fs = require('fs');
const path = require('path');
const filePath = path.resolve(__dirname, 'hello.txt')
console.log('filePath: ', filePath);
fs.readFile(filePath, (err, res) => {
console.log(err, res.toString());
});
Copy the code
Run node with the –inspect-brk flag
With the –inspect-brk flag at the end of node, node.js will listen on the debug client, by default at 127.0.0.1:9229, or display the specified address –inspect-brk=_host:port_
$ node --inspect-brk app.jsThe Debugger listening on the ws: / / 127.0.0.1:9229 / fb c7a51e5a - d9be - 4506-83-0 a9340d2b9ba For help, see: https://nodejs.org/en/docs/inspectorCopy the code
Note the difference between node –inspect and node –inspect-brk: –inspect does not terminate, –inspect-brk terminates before the user code starts, i.e. the code pauses on the first line.
Open it in Chrome
Enter in the browser address barchrome://inspect/Press Enter, as shown below:Remote Target displays the current node.js version. Open itinspect 或 Open dedicated Devtools for NodeThe link, as shown below:
Breakpoint debugging
This section describes the debugging tool window
The top shows the 5 panels required for server-side debugging, which are basically the same as those in Chrome development tools, and can be understood as “server-side customized version”.
- Connection: link
- -Leonard: Console.
- Sources: Source code debugging (this section focuses on)
- Memory: Memory, looking for Memory problems that affect performance, including Memory leaks, Memory bloat, and frequent garbage collection
- Performance Profiler:
The five charts in the upper right are shown from left to right:
- Resume script execution(F8): Resume script execution automatically at the breakpoint each time.
- Step over next function call(F10) : Skip the next function call, execute the current line of code, stop on the next line of code, is a Step by Step operation.
- Step into next function call(F11) : Step into the next function call.
- Step out next function call(F11) : Step out of the next function call.
- Step(F9) : Executes the current line of code and stops on the next line.
To set breakpoints
Under the Source TAB, go to app.js. This is the entry file to our test script. If it executes the –inspect-brk flag, it will default to the first line of code. The first way to set breakpoints is to add them to your programdebugger
The second way to set a breakpoint is by clicking on the line of code you want to set in the editor window. The line is selected in the editor window and has a small arrow on the right. The Breakpoints we set are also shown in the Breakpoints panel on the lower right.Cancel the breakpoint and toggle by clicking again to the left of the selected line of code, or right-click and selectRemove breakpoint
For more information about breakpoint debugging, see Learn More about Chrome DevTools, seeUse breakpoints to pause code
Debug the started Node.js process
What if a Node.js process starts without the –inspect-brk flag, but we don’t want to restart the process for debugging? Either of the following two methods can be used:
Method 1: process._debugProcess(PID)
Find the ID of the currently started Node process and use Node -e ‘process._debugProcess(26853)’ to link process 26853 to the debugger.
$ ps ax | grep app.js26864 s001s + 05:00.01 grep app.js 26853 s002s + 05:00.09 node app.js
$ node -e 'process._debugProcess(26853)'
SIGUSR1
Copy the code
Mode 2: SIGUSR1 signal
Sending SIGUSR1 signals to the Node process can also establish links to debugging tools. Not available on Windows, also note Version, the Inspect API will be activated in Node.js Version 8 or later.
$ kill -SIGUSR1 26853
Copy the code
test
The following changes are made to the Demo. Create an HTTP Server to read a file every time it receives a request. If you start the debugging tool as described above, enter the file in the browserhttp://localhost:3000
When you press Enter, you are automatically redirected to the debug screen. Now you can set breakpoints and debug as above. The current program is running at line 6 of the breakpoint, and when you hover over req.url you will see the value of this property.
Remote debugging
If you are debugging on the server, it is recommended not to let the debugger listen for public IP addresses. This may cause security risks for remote access, but what if you want to debug locally? If you want to allow remote debug links, it is recommended to use SSL tunnel mode, assuming our service is running on the server debug.nodejs.red, start the service first, as above.
$ node --inspect-brk app.js
Copy the code
Setting an SSH Tunnel
Set up an SSH tunnel on the local computer, which will convert links received on port 9221 on the local computer to port 9229 on the server debug.nodejs.red.
$ ssh -L 9221:localhost:9229 [email protected]
Copy the code
Add a link to Connection in Chrome DevTools debugger
By default, there is only one localhost:9229 in Connection. After adding one localhost:9221 we can debug as if we were running node.js locally.
Reference
- chrome-devtools
- debugging-getting-started