docs.nestjs.com icon indicating copy to clipboard operation
docs.nestjs.com copied to clipboard

Put in the support documentation for major editors such as debugging.

Open thiagosantoscunha opened this issue 7 years ago • 13 comments

General Info

It is extremely important to know how to debug the application under development. There are still few materials with reference to NestJs, and so far, version 5 is giving me a great job to debug it in VSCode.

The idea is to put support of the IDEs and main editors in the official documentation, to facilitate the use of the tool. Particularly in "How to debug a NestJS application in VSCode?". Here in Brazil, many use VSCode and Webstorm. And because of the great complicity in setting up the application environment, many end up ignoring NestJS in corporations.

This will ease a bridge and even improve the marketing of NestJS, which although incredible, is still very lacking in the community.

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ x ] Feature request
[ ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

Frequent errors when trying to debug a NestJS environment inside a VSCode workspace

Expected behavior

Integrate a tutorial in the official Documentation, both for VSCode, as for the other IDEs and Editors an environment configuration for debugging.

Minimal reproduction of the problem with instructions

What is the motivation / use case for changing the behavior?

Improve the experience with the developer and the companies in general.

Environment


Nest version: 5.4.0

 
For Tooling issues:
- Node version: 10.13.0
- Platform:  Windows 

Others:

thiagosantoscunha avatar Dec 17 '18 14:12 thiagosantoscunha

Wrong repository, should be here: https://github.com/nestjs/docs.nestjs.com/issues

There are tons of different ways to debug an application, everyone has it's different favorites. Personally I use ts-node as I don't like the explicit transpiling, actively excluding it from git and sometimes it doesn't even transpile the source code which leads to wasted time due to searching for bugs which don't exist.

This is a launch.json in one of my microservices:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
          "type": "node",
          "request": "launch",
          "name": "Request Worker",
          "runtimeArgs": [
              "--nolazy",
              "-r",
              "${workspaceRoot}/node_modules/ts-node/register"
          ],
          "args": [
              "${workspaceRoot}/src/index.ts"
          ],
          "envFile": "${workspaceRoot}/profiles/.env.development",
          "outputCapture": "std"
        }
    ]
}

weeco avatar Dec 17 '18 22:12 weeco

Any updates on this @kamilmysliwiec?

Would love to find a convenient way to debug through VSCode, right now it takes ~20 seconds after running npm run start:debug using the nodemon-debug.json, and it only works when using the --inspect-brk flag which causes you to have to hit the continue button before waiting some more for the debugger to attach to the process.

Tyler-V avatar Mar 28 '19 15:03 Tyler-V

This is the launch.json in my project:

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Nest Framework",
            "runtimeArgs": [
                "--nolazy",
                "-r",
                "ts-node/register"
            ],            
            "args": [
                "${workspaceFolder}/src/main.ts"
            ],
            "autoAttachChildProcesses": true
        }
    ]
}

Notice that setting: autoAttachChildProcesses to true allows seeing Nest output in the terminal. I assume ts-node is installed globally. I hope that helps.

sattinos avatar Apr 17 '19 09:04 sattinos

How is the startup speed @sattinos? I noticed ts-node was taking forever compared to a nrwl/nx nestjs spawned project which carries a different config and doesn't use ts-node.

Literally ~3-5 seconds compared to ~30 when waiting for the project to bootstrap and attach.

Tyler-V avatar Apr 17 '19 12:04 Tyler-V

@Tyler-V Debugger attaching : 0.5 second NestJS terminal logs: 3 seconds

sattinos avatar Apr 17 '19 12:04 sattinos

Even after waiting a long time, with a fresh project on macOS using either node 10.15.3 or node 12.2.0, running npm run start:debug results in only the following output and no application running. I checked htop and the node process this spawns is running at 100% cpu.

[nodemon] 1.19.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /Volumes/Mizzi/Casechek/test-nest/src/**/*
[nodemon] starting `node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts`
Debugger listening on ws://127.0.0.1:9229/40dcd8a6-4ccf-4d80-ba2b-77f50b8387f3
For help, see: https://nodejs.org/en/docs/inspector

Replacing inspect-brk with inspect in nodemon-debug.json:

{
  "watch": ["src"],
  "ext": "ts",
  "ignore": ["src/**/*.spec.ts"],
  "exec": "node --inspect -r ts-node/register -r tsconfig-paths/register src/main.ts"
}

Solved the problem for me and then I was able to hit breakpoints (in PhpStorm).

themizzi avatar May 14 '19 04:05 themizzi

This configuration works for me. Hope this helps someone :)

Make sure you add tsconfig-paths/register line under runtimeArgs else you will get an error saying some of your user defined modules were not found.

Also replace <YOUR_APP_ROOT_FOLDER> name with your application folder name if you have one under your root project folder, else remove it from the path in the script.

Note: Make sure to stop running your app before executing this debug config on vscode, because this debug script will launch a new instance of your app on the same port.

{
    "version": "0.2.0",
    "configurations": [
        {
            "type": "node",
            "request": "launch",
            "name": "Debug Nest Framework",
            "args": [
                "${workspaceFolder}/<YOUR_APP_ROOT_FOLDER>/src/main.ts"
            ],
            "runtimeArgs": [
                "--nolazy",
                "-r",
                "ts-node/register",
                "-r",
                "tsconfig-paths/register"
            ],
            "sourceMaps": true,
            "cwd": "${workspaceRoot}/<YOUR_APP_ROOT_FOLDER>",
            "protocol": "inspector"
        }
    ]
}

sandeepsuvit avatar Jun 12 '19 06:06 sandeepsuvit

Hi all. My current working config for vscode with "start:debug": "nest start --debug" is:

    {
      "name": "NestJS Debug",
      "type": "node",
      "request": "launch",
      "protocol": "inspector",
      "timeout": 25000,
      "port": 9229,
      "runtimeArgs": ["run-script", "start:debug"],
      "sourceMaps": true,
      "cwd": "${workspaceRoot}",
      "runtimeExecutable": "npm",
      "windows": {
        "runtimeExecutable": "npm.cmd"
      },
      "envFile": "${workspaceRoot}/.env",
      "skipFiles": [
        "${workspaceRoot}/node_modules/**/*.js",
        "<node_internals>/**/*.js"
      ],
      "console": "integratedTerminal",
      "outputCapture": "std",
      "autoAttachChildProcesses": true
    },

PinkyRabbit avatar Mar 01 '20 12:03 PinkyRabbit

If you are using nestjs 6.8+ (see here)

add launch.json in .vscode folder with this content

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "type": "node",
      "request": "attach",
      "name": "Attach NestJS WS",
      "port": 9229,
      "restart": true,
      "stopOnEntry": false,
      "protocol": "inspector"
    }
  ]
}

then run npn run start:debug

then in vscode choose Attach NestJs Ws

Done! you can set break point enter image description here

RezaRahmati avatar Mar 09 '20 15:03 RezaRahmati

@RezaRahmati I am using v7.1 and aforementioned setup doesn't works. Because it seems app starts, then debugger is attached and with that breakpoints are skipped for me.

lume-code avatar Apr 15 '20 11:04 lume-code

how to change port other than 9229 such like 9230 when npm run start:debug?

orime avatar Jun 18 '20 04:06 orime

change that script in package.json to

something like this

nest start --debug --watch

ex.

nest start --debug 9660 --watch

fchwpo avatar Jul 13 '20 12:07 fchwpo

You can run nest start --help for more details @Orime112

fchwpo avatar Jul 13 '20 12:07 fchwpo