Debugging Node.js

Visual Studio Code

VS Code provides a debugging client that works without having to use any browser tools.

To get started, we recommend you open the debugging tools (the little bug on a play button in the toolbar) and click the create a launch.json file link, selecting the Node.js environment. It will show you a launch.json file which you can leave mostly unchanged.

You should change the "program" property to point to your main source file, probably index.js or server.js.

You can then add breakpoints by clicking to the left side of the line number in any of your programme files. When you run the debugger (click the green arrow at the top of the debugger pane), the debugger will pause the programme on that line. From there you can view the values of variables, step through the order of execution, and much more.

Understanding how to debug your applications with tools more robust than console.log will put you in the driver seat to explore the entire runtime environment of your application. Be sure you use this capability to increase your understanding.

Nodemon

Often when you're developing, it's useful to be able to make changes and see the results of those changes. In these times, having to stop and restart the server becomes tedious pretty quickly. One option is to have the server automatically restart whenever one of its files are changed. This is what Nodemon does.

To use Nodemon, install it and use it like this:

npm install nodemon
nodemon server

This will run server.js, and restart the server whenever file changes are detected (on save).