So I have been learning Node.js for a while, but debugging is sometimes hard for me, maybe because I am a beginner
So how to debug Node.js with VS Code, at first you need to know the following command, which will listen for a debugging client on port 9229, further info can be found on this link
So I assume you have a Node.js project now and you are using Visual Studio for your coding. Then follow the steps below to use the debugging tool.
1. Go to Preferences -> Settings
2. Enable VS Code Debugging
Search for “node debug” -> Click on “Node debug” under “Extensions” -> Select “on” for “Debug > Node: Auto Attach”, this will turn on the node debugger when you start the Node.js application using the Terminal in VS Code.
3. Start a terminal in VS Code
Click on View -> Terminal or using the shortcut [CTRL + `], for Mac OS keyboard to shortcut Key mapping, please go to end of this tutorial for more information.
4. Run your application using the terminal
Make sure you use the –inspect option, once you start it, it will detect and start a debugging mode
# if use node node --inpspect index.js # if use nodemon nodemon --inspect index.js
5. Debugging View
You should see the button bar turn to orange if the debugging mode is on.