Setting up debugging can be a chore. It can make you feel like console.log isn’t so bad after all. Right? While I enjoy a good console.log as much as anyone, I’d much rather debug with zero configuration of my app. Wouldn't you?
Visual Studio Code (aka VS Code) has changed the game on Node.js debugging. There is a feature called Node: Auto Attach (you can learn more about it here in the docs). Or keep reading — this will only take a minute.
When you enable this feature, you can run your node app from a command line, from an npm script, or from a tool that runs one of those. Just make sure you add the Node Inspector flag to let Node know you are planning on debugging. Once you do this, the debugger lights up in VS Code!
Open the settings in VS Code (CMD + , on Mac or CTRL + , on Windows). You can also go to the Command Palette (CMD + SHIFT + P on Mac or CTRL + SHIFT + P on Windows) and search for settings.
Search for “auto attach” and you’ll see a setting for Node: Auto Attach. Go ahead and turn that on.
Now run your Node.js app from your favorite command line whether that be integrated terminal, external terminal, or using a magic wand (well, if you have a magic wand, you likely don’t need debugging 😀).
node --inspect=0.0.0.0:9229 server.js
Now when you launch your app with the --inspect flag the debugger attaches to your app in VS Code! From here you can set breakpoints, step through your code, and have all the great debugging goodness!
You can learn more about this feature from this link in the VS Code docs.
Cross posted from original article on medium