New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debugging with VSCode #37

Closed
cschroeter opened this Issue Apr 24, 2018 · 11 comments

Comments

Projects
None yet
@cschroeter

cschroeter commented Apr 24, 2018

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

This comment has been minimized.

mpicciolli commented Apr 24, 2018

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

This comment has been minimized.

Member

kamilmysliwiec commented May 13, 2018

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

@yanshuf0

This comment has been minimized.

yanshuf0 commented May 25, 2018

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

This comment has been minimized.

Contributor

mchelen commented Jul 31, 2018

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

@walter211

This comment has been minimized.

walter211 commented Sep 21, 2018

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

This comment has been minimized.

TristanMarion commented Oct 3, 2018

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

This comment has been minimized.

KBalint1991 commented Oct 27, 2018

@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

This comment has been minimized.

kushwahashiv commented Nov 22, 2018

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

This comment has been minimized.

trainerbill commented Nov 27, 2018

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

This comment has been minimized.

orlaqp commented Dec 7, 2018

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

This comment has been minimized.

orlaqp commented Dec 7, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment