You are currently viewing Debugging Multiple NestJS Applications in VSCode

Debugging Multiple NestJS Applications in VSCode

Debugging is a critical part of the development process, and when working with NestJS applications, Visual Studio Code (VSCode) offers tools to make this task easier and more efficient. This guide will walk you through setting up and using VSCode to debug NestJS applications, including how to handle multiple instances.

Setting Up Debug Mode

To begin debugging your NestJS application, you need to configure your environment to start the application in debug mode. Here’s how you can set up your package.json and launch.json in VSCode.

Modifying package.json

Add the following script to the scripts section of your package.json. If you created your project with the NestJS CLI, using nest start this should already be present.

"start:debug": "nest start --debug --watch",
Code language: JSON / JSON with Comments (json)

This script configures your application to start in debug mode and watches for any changes in the source files, automatically reloading the app.

Configuring launch.json

Next, set up your debugging environment by modifying the launch.json file in VSCode:

{
  "name": "My NestJS App",
  "port": 9229,
  "request": "attach",
  "skipFiles": ["<node_internals>/**"],
  "type": "node",
  "restart": true
}
Code language: JSON / JSON with Comments (json)

This configuration enables VSCode to attach to your NestJS application running in debug mode on port 9229.

Running and Debugging the Application

To start debugging, run the following command in your terminal:

npm run start:debug
Code language: Bash (bash)

You should see a log output similar to this:

LOG [NestApplication] Nest application successfully started +0ms
Code language: Bash (bash)

In VSCode, use the Run and Debug feature to attach the debugger using the configuration specified in your launch.json. Once the debugger is attached, you’ll see:

Debugger Attached
Code language: Bash (bash)

Debugging Multiple Instances Simultaneously

If you’re running multiple instances of your application, each instance needs to be run on a different port. Here’s how you can configure this:

Adjusting package.json

For a second instance, modify the debug script in your package.json to specify a different port:

"start:debug": "nest start --debug=9234 --watch",
Code language: JSON / JSON with Comments (json)
Tweaking launch.json

Similarly, update the launch.json for the second instance:

{
  "name": "My Other NestJS App",
  "port": 9234,
  "request": "attach",
  "skipFiles": ["<node_internals>/**"],
  "type": "node",
  "restart": true
}
Code language: JSON / JSON with Comments (json)

Adding Breakpoints

To effectively debug, place breakpoints in your code where you want the execution to pause. This will allow you to inspect the current state, watch expressions, and step through your code.

Conclusion

By following these steps, you can efficiently set up and debug NestJS applications in VSCode, even with multiple instances running simultaneously. This setup not only enhances your debugging capabilities but also speeds up your development process by allowing you to instantly see the effects of your changes.

Have any questions, want to share your thoughts or just say Hi? I’m always excited to connect! Follow me on Twitter or LinkedIn for more insights and discussions. If you’ve found this valuable, please consider sharing it on your social media. Your support through shares and follows means a lot to me! 

Leave a Reply