Introduction to the Node debugging tool getting started tutorial, node debugging tool
JavaScript programs become increasingly complex, and debugging tools become increasingly important. The client script has a browser. How can I debug the Node script?
In 2016, Node decided to use Chrome's "Developer tool" as the official debugging tool, so that Node scripts can also be debugged using the graphical interface, which greatly facilitates developers.
This article describes how to use the Node script debugging tool.
I. Sample program
For convenience, the following is an example script. First, create a working directory and enter it.
$ mkdir debug-demo$ cd debug-demo
Then, generatepackage.json
File, and install the Koa framework and the koa-route module.
$ npm init -y$ npm install --save koa koa-route
Next, create a script.app.js
And write the following content.
// app.jsconst Koa = require('koa');const router = require('koa-route');const app = new Koa();const main = ctx => { ctx.response.body = 'Hello World';};const welcome = (ctx, name) => { ctx.response.body = 'Hello ' + name;};app.use(router.get('/', main));app.use(router.get('/:name', welcome));app.listen(3000);console.log('listening on port 3000');
The above code is a simple Web application with two routes specified. A line of welcome information is displayed after access. For more information about the code, see the Koa tutorial.
2. Start the developer Tool
Now, run the above script.
$ node --inspect app.js
In the code above,--inspect
Parameters are required to start the debugging mode. In this case, open the browser to access http: // 127.0.0.1 // 3000 and you will be able to see Hello World.
Next, start debugging. There are two methods to open the debugging tool. The first method is to enterchrome://inspect
Orabout:inspect
And press enter to view the following interface.
In the Target section, click the inspect link to go To the debugging tool.
The second method to enter the debugging tool is to open "Developer Tools" in the http: // 127.0.0.1 // 3000 window. There is a Node green icon in the top left corner, and you can click to enter.
Iii. debugging tool window
The debugging tool is actually a customized version of the "Developer tool", saving the useless parts of the server script.
It mainly has four panels.
- Console: Console
- Memory: Memory
- Profiler: Performance
- Sources: source code
The usage of these panels is basically similar to the browser environment. Here we only introduce the Sources (source code) panel.
Iv. set breakpoints
Go to the Sources panel and find the running script.app.js
.
Click on the row number of row 11th (that is, the row below) to set a breakpoint.
ctx.response.body = 'Hello ' + name;
When the browser accesses http: // 127.0.0.1: 3000/alice, the page displays waiting for the server to return. Switch to the debugging tool and you can see that the main Node thread is in the paused stage.
Go to the Console panel and enter name. alice is returned. This indicates that we are in the context at the breakpoint ).
Switch back to the Sources panel. On the right side, you can see folding items such as Watch, Call Stack, Scope, and Breakpoints. Open the Scope collapse item and you can see all the variables in the Local Scope and Global Scope.
In the Local scope, variablesname
The value isalice
Double-click to enter the editing status and change itbob
.
Click "continue" in the top toolbar.
Hello bob is displayed on the page.
In the command line, press ctrl + c to terminate the operation.app.js
.
5. debug non-service scripts
Web service scripts will always run in the background, but most of the scripts only process a task and will be terminated after running. At this time, you may not have time to open the debugging tool. When you open the script, it has been running. How can I debug it?
$ node --inspect=9229 -e "setTimeout(function() { console.log('yes'); }, 30000)"
In the code above,--inspect=9229
Set the debugging port to 9229, which is the default communication port of the debugging tool.-e
The parameter specifies a string to run as the code.
Accesschrome://inspect
To debug the code.
Put the code insetTimeout
It is always inconvenient. Scripts that run for a short time may have no time to open the debugging tool. Use the following method.
$ node --inspect-brk=9229 app.js
In the code above,--inspect-brk
Specifies that the breakpoint is set on the first line. That is to say, running at the beginning is the paused state.
6. forget to write -- what should I do with inspect?
The premise of opening the debugging tool is that when the Node script is started--inspect
Parameters. If you forget this parameter, can you debug it?
The answer is yes. First, the script is started normally.
$ node app.js
Then, in another command line window, find the process Number of the above script.
$ ps ax | grep app.js 30464 pts/11 Sl+ 0:00 node app.js30541 pts/12 S+ 0:00 grep app.js
In the preceding command,app.js
The process Number of is30464
.
Then, run the following command.
$ node -e 'process._debugProcess(30464)'
The above command will establish a connection between process 30464 and the debugging tool, and then you can open the debugging tool.
Another method is to send the SIGUSR1 signal to the script process and establish a debugging connection.
$ kill -SIGUSR1 30464
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.