Post/Code

HomeAboutUsesNow

Nest.js Debugging with VS Code.

In order to enable easy Node.js debugging of your Nest.js app from VS Code, do the following.

  • In the root of your project, create a nodemon-debug.json file

        "watch": ["src"],
        "ext": "ts",
        "ignore": ["src/**/*.spec.ts"],
        "exec": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register src/main.ts"
    }
    
  • In your package.json file, create a task as follows "start:debug": "nodemon --config nodemon-debug.json",

  • In your VS Code Preferences: Code -> Preferences -> Search for Auto Attach and enable this.

  • Then, from terminal run: npm run start:debug

VS Code will then auto-attach the debugger and you can develop as usual, with the benefit of running your breakpoints directly from VS Code while still testing and using your app usual.

You will need to keep pressing the "Continue" button in the debugging menu, and the server is slower in operation, but, the benefit of being able to debug right in VS Code is massive! :)

Bonus

When running an Angular app, make sure you create an npm start task that runs the --open flag. This will run your Angular app as well as open it in your default browser! Nice and handy!

"start": "ng serve --open",