How to debug NodeJS apps in VSCode
April 24, 2018
With the latest release of Visual Studio Code, a new feature called Auto Attach was introduced. If enabled, the debugger automatically attaches to the Node process that has been launched from the terminal in VSCode. No configuration required.
Using this feature in your Typescript app is actually pretty simple. If you are already using
ts-node that's perfect. Instead of starting your app like
ts-node script.ts you run
node --inspect-brk -r ts-node/register script.ts from your terminal in VSCode. Now the VSCode will recognize the new node process and attach the debugger automatically. Of course you can encapsulate this command in a npm script like
"start:dev": "node --inspect-brk -r ts-node/register script.ts. Just make sure you are using the integrated terminal.
This Auto Attach feature also works if you are using Nodemon to restart your app after one of your Typescript files has been changed. In your nodemon.json change your exec property to look like
"exec": "node --inspect-brk -r ts-node/register src/main.ts"