nestjs / typescript-starter

Nest framework TypeScript starter :coffee:
https://nestjs.com
1.86k stars 1.05k forks source link

Debugging with VSCode #37

Closed cschroeter closed 6 years ago

cschroeter commented 6 years ago

With the latest release of Visual Studio Code - a new feature called Auto Attach was introduced. To debug your NestJS app enable Auto Attach and edit nodemon.json file to look like this

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

Now just run yarn start:dev and happy hacking!

mpicciolli commented 6 years ago

To debug your JEST tests, the following launch config worked for me ( check your node --version, make sure it's 8.X):

{
    "name": "Debug Jest tests",
    "type": "node",
    "request": "launch",
    "program": "${workspaceRoot}\\node_modules\\jest\\bin\\jest.js",
    "args": [
      "--runInBand",
      "--no-cache"
    ],
    "runtimeArgs": [
      "--inspect-brk"
    ],
    "cwd": "${workspaceRoot}",
    "protocol": "inspector",
    "console": "integratedTerminal"
  }
kamilmysliwiec commented 6 years ago

I added nodemon-debug.json and npm run start:debug command based on your configuration. Thanks 👍

yanshuf0 commented 6 years ago

This is a very important post, someone should contribute to the documentation (I don't mind writing it if that helps) to walk people through this.

Heads up, using gitbash (or any other multiplexer) as your integrated terminal will NOT trigger the Auto Attach debugging capabilities. From the above article:

"Please note that this feature does not (yet) work for terminal multiplexers like 'tmux' (where launched processes are not children of VS Code's integrated terminal)."

mchelen commented 6 years ago

This is a great feature, can someone please document how to use it? @yanshuf0 @kamilmysliwiec

walter211 commented 6 years ago

dont know why stoped in seconds and showing these:

/usr/local/bin/npm run-script start:dev Debugger listening on ws://127.0.0.1:9229/91f16f11-51c8-40ed-a4fa-91bce55111f6 Debugger attached. Debugger listening on ws://127.0.0.1:9229/04e094c1-fdfe-4c9f-82b5-a3352a41e38d

TristanMarion commented 5 years ago

Hello, I try to debug my Nest applications in Docker containers, I can connect to the debug socket but my breakpoint does not work. The only way to get them working at the moment is to compile the files and then run the app using node and not ts-node. If someone has already achieved what I try to do, don't hesitate to ping me 😄

I can send my configuration for the node solution if someone needs it :)

KBalint1991 commented 5 years ago

@walter211 I have the same issue, it seems that --inspect-brk is just not starting the app properly. It works fine with --inspect, but I want to use breakpoints.

The app which should be running on the 3000 port is not starting.

Could you solve it anyhow?

My output:

[nodemon] starting `ts-node --inspect-brk -r ts-node/register src/main.ts`
Debugger listening on ws://127.0.0.1:9229/3fd6a281-84ba-4c42-9702-a305cb7494c0

Node version: 9.8.0 Nodemon version: 1.17.3 VSCode version: 1.28.2

kushwahashiv commented 5 years ago

I added nodemon-debug.json and npm run start:debug command based on your configuration. Thanks 👍

this doesn't work for me. I tried https://github.com/nestjs/typescript-starter as well it do not work with that.

Please let me know if anything I'm missing here.

thanks.

trainerbill commented 5 years ago

I added 2 run scripts for debugging tests:

"test:debug": "node --inspect-brk node_modules/.bin/jest",
"test:e2e:debug": "node --inspect-brk node_modules/.bin/jest --runInBand --config ./test/jest-e2e.json"

@kamilmysliwiec can something similar be added to the typescript-starter?

orlaqp commented 5 years ago

Hi everyone I am using the new node nodemon.json and it looks like this:

{
  "watch": ["apps/api/src"],
  "ext": "ts",
  "ignore": ["apps/api/src/**/*.spec.ts", "apps/api/src/app/graphql.ts"],
  "exec": "ts-node -P apps/api/tsconfig.app.json -r tsconfig-paths/register apps/api/src/main.ts"
}

It does not have the original path because I am using a monorepo and move the application to a different folder.

Now in my package json I have the following line:

"api:start:debug": "nodemon --config ./apps/api/nodemon-debug.json",

When I run (with the VSCode Auto Attach feature enabled):

yarn api:start:debug

I get the following error:

import { NestFactory } from '@nestjs/core';
       ^

SyntaxError: Unexpected token {
    at new Script (vm.js:83:7)
    at createScript (vm.js:267:10)
    at Object.runInThisContext (vm.js:319:10)
    at Module._compile (internal/modules/cjs/loader.js:685:28)
    at Module.m._compile (/Users/orlaqp/development/binaryways/courses/agency-x/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:733:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/Users/orlaqp/development/binaryways/courses/agency-x/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:620:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
    at Function.Module._load (internal/modules/cjs/loader.js:552:3)

Not sure if I provided all the relevant details but do you see here anything I am doing wrong?

Thanks!

orlaqp commented 5 years ago

I was able to fix my issue doing the following:

{
  "watch": ["apps/api/src"],
  "ext": "ts",
  "ignore": ["apps/api/src/**/*.spec.ts", "apps/api/src/app/graphql.ts"],
  "exec": "TS_NODE_PROJECT='./apps/api/tsconfig.app.json' node --inspect-brk -r tsconfig-paths/register -r ts-node/register apps/api/src/main.ts"
}

Basically, I had to add the TS_NODE_PROJECT env var and also register tsconfig-paths

I hope it can help somebody else ...

Tyler-V commented 5 years ago

Started a new project last week off of master and can confirm it is working as advertised in VSCode 1.30.1 using Node 8.9.3.

Don't forget to go to File -> Preferences -> Settings and search 'Auto Attach' it defaults to off.

Run npm run start:debug and set breakpoints in VSCode!

fvilers commented 5 years ago

Great tip! Any idea on how to disable the automatic step in in main.ts? It's annoying to hit F5 on each restart...

rompetomp commented 5 years ago

@fvilers You should be able to use --inspect instead of --inspect-brk

https://nodejs.org/en/docs/guides/debugging-getting-started/#command-line-options

paulvanbladel commented 5 years ago

@Tyler-V ON which OS was that? I'm running on windows but it doesn't work :(

gmcelhanon commented 5 years ago

I couldn't get this to work on Windows 10 (not sure if that's relevant), but I was getting an error at startup (using node start:debug) saying that it couldn't find "src/main.ts".

I changed the "exec" line in nodemon-debug.json from:

  "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register src/main.ts"

... to ...

  "exec": "node --inspect-brk -r ts-node/register -r tsconfig-paths/register ./src/main.ts"

(Added "./" as a prefix on the "src/main.ts" path.)

Now, debugging is working.

paulvanbladel commented 5 years ago

@gmcelhanon Thanks for sharing. Unfortunately, not working on my side. I even don't get your error (just with a fresh nestjs project generated from the CLI. Did you do anything special to get that error msg (additional logging or so?)

When I run npm run start:debug, the output is:

[nodemon] 1.18.9
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: F:\nest-debug\server\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/e466138c-f097-48b1-a322-216d195d142e
For help see https://nodejs.org/en/docs/inspector

I'm running node v8.12.0 vscode v1.30.2

atul221282 commented 5 years ago

@paulvanbladel - I am also experiencing the same issue

But if you change your line in nodemon-debug.json file to

"exec": "node --inspect -r ts-node/register -r tsconfig-paths/register ./src/main.ts"

App runs but no debugging work

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

hgezim commented 5 years ago

Any luck @TristanMarion ?

davingreen commented 5 years ago

I seem to also be having an issue with debugging in VS code...

Windows 10 VS Code Version: 1.35.1 Node: 10.16 Auto Attach: On

nodemon.js (for reference)

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

nodemon-debug.js

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

Steps to reproduce:

  1. npm run start:debug
    [nodemon] 1.19.0
    [nodemon] to restart at any time, enter `rs`
    [nodemon] watching: C:\{...}\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/aa76e8a1-94ac-43ec-8d94-285f4cef8d7e
    For help, see: https://nodejs.org/en/docs/inspector
    Debugger attached.
  2. Breakpoint in main.ts is hit and stops, I continue...
    [Nest] 14964   - 06/26/2019, 6:47 PM   [NestFactory] Starting Nest application...
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] ConfigModule dependencies initialized +425ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] TypeOrmModule dependencies initialized +3ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] AppModule dependencies initialized +178ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] CqrsModule dependencies initialized +2ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] TypeOrmCoreModule dependencies initialized +101ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] TypeOrmModule dependencies initialized +6ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [InstanceLoader] DocumentModule dependencies initialized +17ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [RoutesResolver] AppController {/api/v1/}: +40ms
    [Nest] 14964   - 06/26/2019, 6:47 PM   [RouterExplorer] Mapped {/, GET} route +5ms
    ...(other routes)...
    [Nest] 14964   - 06/26/2019, 6:47 PM   [NestApplication] Nest application successfully started +33ms
  3. Request: GET http://localhost:3000/api/v1/
  4. Breakpoints ignored (not working or stopping on breakpoints)

Any help is appreciated... I've also tried several combinations of manually configuring the debug launch.js, but nothing has worked.

DimaMorgun commented 5 years ago

@davingreen I've resolved this issue right now. It works for me.

The simpliest way to start debugger. Add to launch.json default node attach configuration { "type": "node", "request": "attach", "name": "Attach", "port": 9229 }

After this I've started application using default command npm run start:debug. Then nodemon started hosting debug environment on 'ws://127.0.0.1:9229/afc915d8-6e9a-433f-92ac-7543589716f5'

And then I've start my debugger in VSCode.

This process was attached to nodemon and breakpoints already works.

After this I can visit my routes, like localhost:3000/amazingApi/v99/doAll.

xieyihao commented 4 years ago

Started a new project last week off of master and can confirm it is working as advertised in VSCode 1.30.1 using Node 8.9.3.

Don't forget to go to File -> Preferences -> Settings and search 'Auto Attach' it defaults to off.

Run npm run start:debug and set breakpoints in VSCode!

thx! It work for me!

Hwan-seok commented 4 years ago

Alternatively,

Use node --inspect -r ./dist/main.js when using Wepack Hot Module Replacement

raedwa01 commented 4 years ago

I am still having trouble. I have the autoattach turned on for VSCode. I run the npm run start:debug which calls "NODE_ENV=development nest start --debug --watch"

The problem is, I get some popup errors as seen in my attached image and even though VSCode says it is autoattached, none of my breakpoints are loaded. DebuggerErr

kamilmysliwiec commented 4 years ago

@raedwa01 try updating @nestjs/cli to the latest version 7.1.0 and using --debug-brk flag instead

raedwa01 commented 4 years ago

--debug-brk is listed an an unknown option. Upgrading to 7.1.1 didn't seem to do anything to help either.

dsbert commented 4 years ago

I am seeing the same errors as @raedwa01 Not sure if this is because of something that changed in vscode.

raul-j commented 2 years ago

--debug-brk is listed an an unknown option. Upgrading to 7.1.1 didn't seem to do anything to help either.

It seems not fixed yet in version 8