Home > Development Tools > VSCode > How to debug node.js in vscode

How to debug node.js in vscode

王林
Release: 2020-02-12 13:50:07
Original
3536 people have browsed it

How to debug node.js in vscode

In vscode, click the spider button

How to debug node.js in vscode
to see the debug sidebar, and then add the configuration

How to debug node.js in vscode
Select the environment

How to debug node.js in vscode
You will see the launch.json file.

How to debug node.js in vscode
When starting, select the corresponding configuration, and then click the green triangle pointing to the right

How to debug node.js in vscode
launch mode and attach mode

How to debug node.js in vscode

When request is launch, it is launch mode. This is the program started from vscode. If it is debugging, it will always be in debugging mode. The attach mode is to connect to an already started service. For example, if you have started the project outside and suddenly need to debug it, you do not need to close the started project and restart it in vscode. As long as you start it in attach mode, vscode can connect to the already started service. When debugging is over, just disconnect, which is obviously more convenient than launch.

Use npm to start in debug

Many times we write long startup commands and configurations in the scripts of package.json, such as:

How to debug node.js in vscode

We want vscode to start and debug using npm, which requires the following configuration:

How to debug node.js in vscode

Use nodemon to start in debug

Only use npm to start. Although nodemon is used in the dev command, the program can be restarted normally. However, after restarting, debugging is disconnected. So you need to let vscode use nodemon to start the project.

How to debug node.js in vscode

Pay attention to the runtimeArgs here. If these configurations are written in package.json, it will be like this

nodemon --inspect --exec babel-node --presets env ./bin/www
Copy after login
Copy after login

This is very convenient, the project can Normal restart, the debugging function will be enabled every time you restart.

But what should we do if we don’t want to enable the debugging function all the time? This requires using the attach mode mentioned above.

Use the following command to start the project normally

nodemon --inspect --exec babel-node --presets env ./bin/www
Copy after login
Copy after login

When we want to debug, run the following configuration in the debug of vscode

How to debug node.js in vscode

Related recommendations: vscode tutorial

The above is the detailed content of How to debug node.js in vscode. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template