Try   HackMD

version française

Node.js debugger into VS Code

node --inspect

To enable Node's debug mode, we should execute node with the --inspect flag.

Let's add that flag to our npm run dev script into package.json file, generated by irongenerate:

"dev": "DEBUG=starter-code:* nodemon --inspect ./bin/www"

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
Without having to modify that line, we can also pass the flag by executing the command npm run dev -- --inspect but it's longer
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
and we have to remember it every time (see the npm-run-script doc about.)

Auto Attach

In VS Code's preferences, we set the Node: Auto Attach setting to on:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

Debug now🐛

From a VS Code terminal, we now launch the command npm run dev:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

The command outputs: Debugger listening on port 9229.

We can now set some breakpoints, by clicking on the line we want to debug:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

🔴 The red point to the left of the line 71 indicates that a breakpoint is now set to.


When a request will call that line of code, the debugger will pause the program and will allow us to inspect our variables values, do some step-by-step etc:

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

devTools alternative

If we don't want to use VS Code to debug, Chrome devTools is also possible:

  • Go to URL: chrome://inspect
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • then click on the inspect link on the corresponding remote target :
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →