nx: node --inspect/--inspect-brk incompatible with nx task runner

Current Behavior

on node --inspect-brk ./node_modules/.bin/nx build best-app, post attach, I cannot debug due to port conflict:

Debugger listening on ws://127.0.0.1:9229/10537d99-1102-40aa-8820-a5993dce9841
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

> nx run best-app:build
Starting inspector on 127.0.0.1:9229 failed: address already in use
Waiting for the debugger to disconnect...

Port 9229 is otherwise free–it is not used until this very node process begins.

Expected Behavior

node --inspect-brk $(yarn bin)/nx build best-app to allow me to debug my application’s build pipeline

Steps to Reproduce

reproduction and instructions can be found here: https://github.com/cdaringe/nxtester/tree/demo/no-debugging

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 4
  • Comments: 18 (3 by maintainers)

Most upvoted comments

@isaacplmann Got debugging working in VSCode. I’d be happy to collaborate on a debugging techniques document if you want to make a template I can add to.

@jkolyer it seems that other people have found solutions. What specific command are you having trouble debugging? I always use the Javascript Debug Terminal in VS Code to debug things.

  1. Command-shift-P and type in debug. Then choose Javascript Debug Terminal
  2. Set a breakpoint in your code (either with debugger; or using VS code’s breakpoints)
  3. In the debug terminal that opened, run your normal comand (i.e. nx run my-app:build)

Here are some docs talking about this method: https://nx.dev/latest/angular/generators/workspace-generators#with-visual-studio-code

Thanks for reporting the issue 😄

This is due to fork being used to run the command… We’ll have to investigate how to handle this…

If you want to debug the actual build… you should inspect the tao directly.

node --inspect-brk node_modules/.bin/tao build best-app

I think it’s worth to mention that debugging preferences is also available via project.json files.

As you can see in the image, I’m runnning 3 different projects (Express API) on diferent ports and debugging 2 of them (the other one is disabled for example purposes).

Debugging 1st app on port 9230

image

Debugging 2nd app on port 9231

image

Last app with debugging disabled

image

All 3 apps running alltogether

image

More details at NX Website

Sorry to post on closed issue but I encounter the same issue as well. I need to debug tests and take memory snapshots. When launch node --inspect-brk ./node_modules/@nrwl/cli/bin/nx test backend It outputs

Debugger listening on ws://127.0.0.1:9229/54183c9a-66ed-4a2c-93c7-98f4788da4c2
For help, see: https://nodejs.org/en/docs/inspector

Then I go to chrome://inspect Click on Open dedicated DevTools for Node https://prnt.sc/12m6csh

It opens devtools and steps into Sources tab automatically. There I can see some code which seems to be coming from some nx files. 1 Is it proper behavior?

Then I get an error

Starting inspector on 127.0.0.1:9229 failed: address already in use

If I try to take any snapshots it just hangs on a random percentage value and debugger is disconnected (I cannot see nx workspace in chrome://inspect anymore. Tests continue to run and I cannot stop them with ctrl + c.

Please help me. I spent nearly one day on this issue and still unable to debug any tests.

node --inspect-brk=0 ./node_modules/@nrwl/cli/bin/nx test libname works but in a little bit unexpected way. First run spins debugger on port 38687

Debugger listening on ws://127.0.0.1:38687/3c212232-bde3-4fc8-8362-66f251bdd776
For help, see: https://nodejs.org/en/docs/inspector
Debugger attached.

After going to devtools and pressing on Resume script execution it runs the following script

 nx run backend:test --no-cache

Now debugger is listening on another port

Debugger listening on ws://127.0.0.1:38379/78210d4c-ee77-4cf0-a305-cb22901857a2
For help, see: https://nodejs.org/en/docs/inspector

Go to chrome://inspect and add this new port by pressing Configure button. dev tools window opens and goes to sources tab automatically. Now the file name is run-cli.js Remote Target now contains two items 2

Resume script execution this time in run-cli.js Now able to take some snapshots

There are two things you can do:

  1. NX_SKIP_TASKS_RUNNER=true node --inspect ./node_module/.bin/nx build blah will not fork the process. You should really avoid doing this.
  2. node --inspect=0 ./node_module/.bin/nx build blah will use a random port as @june07 described.

We cannot really fix because we need to create multiple processes to enable caching and when using run-many.

@isaacplmann can you make we have a guide in the docs talking about debugging?