DEV Community

John Papa
John Papa

Posted on • Originally published at Medium on

Debug Your Node.js App in 60 Seconds

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?

Sketchnote of Debugging Node.js in 60 Seconds

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!

Auto-Attach the Debugger to Node.js Apps with VS Code

Step by Step

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
Enter fullscreen mode Exit fullscreen mode

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.

Auto-Attach


Cross posted from original article on medium

Top comments (7)

Collapse
 
qm3ster profile image
Mihail Malo

I never knew! Works with mocha inspect too.

Collapse
 
john_papa profile image
John Papa

glad it helped!

Collapse
 
josef profile image
Josef Aidt

Command palette is Ctrl/Cmd + Shift + P
Ctrl/Cmd + P is the file picker, but you can still access the command palette by typing in > in that menu

Collapse
 
john_papa profile image
John Papa

good catch on my typo. thx

Collapse
 
jeasonstudio profile image
Jeason

I like your illustrations! How did you make it?

Collapse
 
john_papa profile image
John Papa

Thank you. I use gel pens and moleskines

I should write about it.

Collapse
 
jamesmh profile image
James Hickey

Sweet! Awesome tip @john_papa !