nestjs / docs.nestjs.com

The official documentation https://docs.nestjs.com 📕
MIT License
1.2k stars 1.74k forks source link

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

Open thiagosantoscunha opened 5 years ago

thiagosantoscunha commented 5 years ago

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:

weeco commented 5 years ago

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"
        }
    ]
}
Tyler-V commented 5 years ago

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.

sattinos commented 5 years ago

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.

Tyler-V commented 5 years ago

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.

sattinos commented 5 years ago

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

themizzi commented 5 years ago

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).

sandeepsuvit commented 5 years ago

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"
        }
    ]
}
PinkyRabbit commented 4 years ago

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
    },
RezaRahmati commented 4 years ago

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

manlyman29 commented 4 years ago

@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.

orime commented 4 years ago

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

fchwpo commented 4 years ago

change that script in package.json to

something like this

nest start --debug --watch

ex.

nest start --debug 9660 --watch

fchwpo commented 4 years ago

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