How does nodejs code debug breakpoints?

Time:2021-8-21

How does nodejs code debug breakpoints?

As a server developer, it is the most basic ability to debug the code at breakpoints. Next, I will briefly introduce two debugging methods, one is dependent on the editor tool, the other is dependent on the Chrome browser (both methods are on the premise that nodejs is installed).

1、 Using the vscode editor

Open the editor and create a new projectvscode-debugger-test, create two files:
app.js:
How does nodejs code debug breakpoints?
test.js:
How does nodejs code debug breakpoints?

If you want to debugtest.jsinlogThis method, you should first create one in the root directory of the current project.vscodeFolder, and then.vscodeCreate a folder namedlaunch.jsonFile:
How does nodejs code debug breakpoints?

The next step is to write this JSON file:

launch.json

{
    //The configuration name will be displayed in the drop-down menu of startup configuration
    "name": "test",
    //Configuration type
    "type": "node",
    //Request configuration type, which can be launch or attach
    "request": "launch",
    //The path to the program that will be debugged
    "program": "${workspaceFolder}/app.js",
    //The command line parameters passed to the program during program debugging are generally set to null
    "args": [],
    //Console execution mode 1. Integrated terminal editor internal terminal execution 2. External terminal external terminal execution
    "console": "integratedTerminal"
}

After editing, start debugging. Firsttest.jsfilelogAdd a breakpoint at the front of the first line inside the function,

How does nodejs code debug breakpoints?
Then click Run on the left side of the editor,
How does nodejs code debug breakpoints?
Then click the run button to start running,
How does nodejs code debug breakpoints?
You can see that the breakpoint has been reached,
How does nodejs code debug breakpoints?
Then you can debug nodejs like JS in the browser.
How does nodejs code debug breakpoints?
Click here to skip breakpoints, stop debugging, step-by-step skip, step-by-step debugging, etc.

Reference: Official launch.json configuration description

2、 Using Chrome’s inspect debugging

Or use the topvscode-debugger-testThis project is used as a demo, but you shouldapp.jsThe document is slightly modified as follows:

How does nodejs code debug breakpoints?

test.jsFile, add one line of code:
How does nodejs code debug breakpoints?

Next, run on the terminalnode --inspect=9229 app.js

After startup, enter in the browser address barhttp://localhost:9999/, as shown below:

How does nodejs code debug breakpoints?

At this point, a new chrome tab is opened and the address bar is entered
chrome://inspect/#devices
How does nodejs code debug breakpoints?

You can see that a link appears here. Click to enter the chrome console, and then refreshhttp://localhost:9999/At this time, you enter the breakpoint:

How does nodejs code debug breakpoints?

It is already very familiar here. Just like debugging JS, debugging is OK.