Debugging Node.js in VSCode
Tags: javascript, node, beginners
Published: 2019-12-15 21:14:13


We use a debugging tool, a 'debugger', to track what’s happening in our code line by line, so we can find out where our errors, 'bugs', live and kindly release those cute little guys from our code.

![](https://media1.giphy.com/media/dM8fBhhhAmbaU/giphy.gif)

Node.js is 'an open-source, cross-platform, JavaScript runtime environment that executes JavaScript code outside of a browser.'^1 And VSCode has a built in debugging environment for debugging our Node code outside of the browser.^2

I know that when I started working in Node.js, I was already familiar with debugging in the browser with Google's DevTools when working with the front-end. Comparing the two helped me feel more comfortable with debugging in VSCode. It turns out the tools are very similar. Here's a side by side comparison (gif sources below):

![](https://storage.googleapis.com/webfundamentals-assets/updates/2018/01/old-worker-stepping.gif)^3
![](https://code.visualstudio.com/assets/docs/nodejs/nodejs-debugging/loaded-scripts-explorer.gif)^4

in browser:
1. open devtools panel
2. set a breakpoint
3. run code
4. step through

with node in vscode:
1. open debugger's panel
2. add config / file path
3. set a breakpoint
4. start debugger
5. step through

Similarities: 
Open some kind of panel, set your breakpoints, start your debugger, step through, rinse and repeat.

Differences:
Location of panel, location of variable tracking, need for a configuration, setting file path, server starting.

For both the browser DevTools and VSCode's debugging panel, we have to open some kind of tool. In VSCode, there's a little bug icon pointing the way or w/ `CTRL + SHIFT + D`

![](https://i.ibb.co/TB02XFJ/debbuggy.png)

In VSCode, we have to set up our config and file path. This points our debugger to the file we need to be read. Usually some default will pop up when you click Add Config, but if not, there will be options for the kind of config needed. 

![](https://i.ibb.co/7tcmFjk/launch.png)

![](https://media2.giphy.com/media/p8ehGO66H5PAQ/source.gif)

Then you can add any breakpoints you might need just like you would in Google's DevTools.

![](https://i.ibb.co/bH6BddF/brk.png)

Start your debugger for the first time with the play button next to where you added your configs. You can restart your debugger later from the debugger's tool bar that will appear. This is also where you'll do your stepping and stopping and playing. If you're unfamiliar with these commands, see the 3rd link below. 

![](https://i.ibb.co/P1529yk/bugbar.png)

Any variables in the file will be listed in the side panel and you can open up different parts by clicking on the down arrow, as seen below. This is just like the Scope panel in your DevTools.

![](https://i.ibb.co/ZMDvwYG/brk2.png) 

If you're using Node.js to build a server and have a config set up to the file that starts your server, the debugger will run this code and restart your server for you, which can become super useful when developing, so you aren't constantly running npm start. Once the server is started, you can go to your page and interact with the page as a user and access any console logs you might have set for GET and POST requests, etc. This will be output in the Debug Console pictured here: 

![](https://i.ibb.co/35X5Nwx/ALm1l90llwpk2-Hz-Mx-WRbj-X68-XYAku-HHKwy-UXBA4-HID7-5-CNnzb-T2dzwu-HFD4-YOLomy-ATKs-Fagf-NIWi2-Ws-UMPn-K0-RVg-KCZ7-QDLq0-Der1-Ka9ok6g0-Nz-Gz-ZSr6-U-ostrwz-Hj-OSl4he-SLeo.gif)

open, config, break, start, step, rinse, repeat

![](https://media1.giphy.com/media/DoL6h5sd6lqZW/source.gif)



[1][node.js docs](https://nodejs.org/en/docs/)
[2][node.js inspect](https://nodejs.org/de/docs/guides/debugging-getting-started/)
[3][DevTools gif](https://developers.google.com/web/updates/2018/01/devtools)
[4][VSCode gif](https://code.visualstudio.com/docs/nodejs/nodejs-debugging)