Nest: Unable to debug app in VS CODE

Created on 22 Sep 2018  路  18Comments  路  Source: nestjs/nest

I'm submitting a...


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

Current behavior


starting with npm run:debug, the client receives a connection refused response and console log on nest.js stops:

MBPFUEKOR:silk-server fuekor$ npm run start:debug

> [email protected] start:debug /projects/silk-erp/silk-server
> nodemon --config nodemon-debug.json

[nodemon] 1.18.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /projects/silk-erp/silk-server/src/**/*
[nodemon] starting `node --inspect-brk -r ts-node/register src/main.ts`
Debugger listening on ws://127.0.0.1:9229/4db21849-1288-418b-8eae-4acdddd22e01
For help see https://nodejs.org/en/docs/inspector 

Debug points set but not reached.

Expected behavior


Debugging code :)

Minimal reproduction of the problem with instructions


My app uses logging, typeorm and swagger so far, but problem can be replicated with any standard app from the examples without modification.

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

Environment


Nest version: 5.1.0


For Tooling issues:
- Node version: 9.21, also with 8.9.x  
- Platform:  
MAC
Others:
VS CODE 1.27.2
When using debugger start from within VS CODE, the following appears in the debugger console:

Debugger listening on ws://127.0.0.1:14272/47b9aa5f-32ce-4800-85f2-c503fb6bb7ce
Debugger attached.


question 馃檶

Most helpful comment

First, remove your launch.json file.

Then open the command ctrl + shift + p and search for Debug: Toggle auto attach, press enter.

Make sure that you've got the following at the bottom of VSC:

image

Add a nodemon-debug.json file at the root of the project with the following content:

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

Add that line in your package.json script section: "start:debug": "nodemon --config nodemon-debug.json"

And now just run
npm run start:debug, should be good :+1:

Credit: Took a lot of that from https://github.com/nestjs/typescript-starter

All 18 comments

After a while debugger windows shows that and debugger crashes:

 1: node::Abort() [/usr/local/bin/node]
 2: node::Chdir(v8::FunctionCallbackInfo<v8::Value> const&) [/usr/local/bin/node]
 3: v8::internal::FunctionCallbackArguments::Call(void (*)(v8::FunctionCallbackInfo<v8::Value> const&)) [/usr/local/bin/node]
 4: v8::internal::MaybeHandle<v8::internal::Object> v8::internal::(anonymous namespace)::HandleApiCallHelper<false>(v8::internal::Isolate*, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::HeapObject>, v8::internal::Handle<v8::internal::FunctionTemplateInfo>, v8::internal::Handle<v8::internal::Object>, v8::internal::BuiltinArguments) [/usr/local/bin/node]
 5: v8::internal::Builtin_Impl_HandleApiCall(v8::internal::BuiltinArguments, v8::internal::Isolate*) [/usr/local/bin/node]

BTW: enableCors() is in main.ts.

Can you show the contents launcher.json of your VSCode?

Good Morning,

thanks for the reply.. Below is the launch.json file. AS a remark. Behavior is the same when using the npm run start debug from the command line, which is defined as followed in the package.json:

"start:debug": "nodemon --config nodemon-debug.json",

{
    "version": "0.2.0",
    "configurations": [
      {
        "type": "node",
        "request": "launch",
        "name": "Nest Debug",
        "protocol": "inspector",
        "sourceMaps": true,
        "stopOnEntry": true,
        "console": "internalConsole",
        "program": "${workspaceFolder}/src/main.ts",
        "preLaunchTask": "tsc: build - tsconfig.json",
        "outFiles": [
          "${workspaceFolder}/dist/**/*.js"
        ]

      }
    ]
  }

Greets from Manila
Ralph

Well I was a bit confused how you start the app, because you said

Debug points set but not reached.

Which is obvious if you don't attach the VSCode debugger. Did you try to clone this repository and run it with npm run start

https://github.com/nestjs/typescript-starter

? I would be surprised if this wouldn't work for you

OK will try it out now and let you know.

Same, as soon i use npm run:start:debug startup window stops earluy and browser receives connection refused:
Normal startup:

^CMBPFUEKOR:project fuekor$ nodemon
[nodemon] 1.14.12
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /projects/learning/nest/project/src/**/*
[nodemon] starting `ts-node -r tsconfig-paths/register src/main.ts`
[Nest] 5177   - 2018-9-23 08:03:54   [NestFactory] Starting Nest application...
[Nest] 5177   - 2018-9-23 08:03:54   [InstanceLoader] AppModule dependencies initialized +9ms
[Nest] 5177   - 2018-9-23 08:03:54   [RoutesResolver] AppController {/}: +20ms
[Nest] 5177   - 2018-9-23 08:03:54   [RouterExplorer] Mapped {/, GET} route +3ms
[Nest] 5177   - 2018-9-23 08:03:54   [NestApplication] Nest application successfully started +2ms

debug startup:

> nodemon --config nodemon-debug.json

[nodemon] 1.18.4
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: /projects/learning/nest/project/src/**/*
[nodemon] starting `node --inspect-brk -r ts-node/register src/main.ts`
Debugger listening on ws://127.0.0.1:9229/b38322a3-4d58-450e-832a-e5fbe3800f4d
For help see https://nodejs.org/en/docs/inspector

I guess, i make a stupid mistake, but all my other node (express, hapi) apps are fine with that.
Thanks for any help :)

So just for clarification:

npm run start does work for you, while npm run start:dev does not work for you in the typescript-starter repository without modifying any of the repository's code?

yes

First, remove your launch.json file.

Then open the command ctrl + shift + p and search for Debug: Toggle auto attach, press enter.

Make sure that you've got the following at the bottom of VSC:

image

Add a nodemon-debug.json file at the root of the project with the following content:

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

Add that line in your package.json script section: "start:debug": "nodemon --config nodemon-debug.json"

And now just run
npm run start:debug, should be good :+1:

Credit: Took a lot of that from https://github.com/nestjs/typescript-starter

i will supplement @maxime1992 answer.
according to Auto Attach docs
npm run start:debug should be executed from VS Code's Integrated Terminal

Good Morning,
sorry i was out of the office last week, but i tried and it works like a charm now using maxime1992's approach.
Thanks for the help
Best Regards from Manila
Ralph.

Using Auto Attach and start:debug to debug graphql app causes nodemon to keep restarting infinitely. Any suggestions?

You should probably open a new issue. Seems very specific and this one's closed :wink:

Nvm, I solved this by putting the generated graphql schema ts file in a separate folder and exclude the folder from nodemon, thanks anyway.

Even running npm run start:debug on VS Code integrated terminal, the debug it's not working.

When i run the debug task with the vs code npm plugin it works.

image

My nodemon-debug.json

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

Following the steps which maxime1992 is saying, breakpoints are not working. This is an issue of VSCode debugger, not Nestjs. Debugging in WebStorm or chome debugger is fine.

Maybe for the people who are trying out new. This is the current configuration for nestjs that I have

launch.json


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

This is my nodemon-debug.json

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

And this is the package.json - a gist of scripts configured


 "scripts": {
    "webpack_dev": "rimraf dist && webpack --mode=development --config webpack.dev.common.js ",
    "webpack_prod": "rimraf dist && webpack --mode=production --config webpack.prod.common.js",
    "format": "prettier --write \"src/**/*.ts\"",
    "start": "ts-node -r tsconfig-paths/register src/main.ts",
    "start:dev": "nodemon",
    "start:debug": "nodemon --config nodemon-debug.json",
    "start:prod": "node dist/server.js",
    "lint": "tslint -p tsconfig.json -c tslint.json",

I use npm run start:debug and then start the degugger that I named ' Debug Nestjs Framework' configured in launch.json and it works like a charm

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings