Node: Is it possible to debug nodejs server with workers?

Created on 31 Oct 2019  路  3Comments  路  Source: nodejs/node

Is it possible to debug workers? Every time I try, I get:

Starting inspector on 127.0.0.1:49344 failed: address already in use
Error: child process exit with code 12: connection problem

As soon as worker is created.

I use VS Code and launch.json looks like:

    {
      "type": "node",
      "request": "launch",
      "name": "Debug gdmn-back",
      "cwd": "${workspaceRoot}/src/gdmn-back",
      "program": "${workspaceRoot}/src/gdmn-back/src/index.ts",
      "outFiles": [
        "${workspaceRoot}/src/gdmn-back/dist/src/**/*.js"
      ],
      "console": "externalTerminal",
      "internalConsoleOptions": "openOnSessionStart",
      "autoAttachChildProcesses": true
    },

I think, if it is NOT POSSIBLE at all, it should be printed with bold letters at the start of corresponding chapter of Nodejs docs. So, there will be no need to spend days searching internet for a solution which doesn't exist.

If there is a way, then it would be good to add it to official docs. As both nodejs and vscode web sites don't say a word about the topic.

I love Nodejs, but inability to debug workers code makes me mad ((

Most helpful comment

Seems it works for me:

  1. open command prompt with admin rights
  2. yarn global add ndb
  3. cd to project folder
  4. yarn rebuild
  5. ndb node ./dist/src/index.js (path will depend on project!)
  6. if you need to set break point before project starts open ndb with command ndb ., browse to sources and put break point, then follow #5.

All 3 comments

@gsbelarus Are you talking about worker_threads workers or cluster workers?

If it鈥檚 worker_threads workers, Chrome DevTools doesn鈥檛 support them yet but e.g. https://github.com/GoogleChromeLabs/ndb/ and some IDEs do.

If it鈥檚 cluster workers, yes, that is possible. If necessary, you can manually specify a port using inspectPort: https://nodejs.org/api/cluster.html#cluster_cluster_settings

We use worker_threads. I will evaluate ndb. Thanks for the link!

Seems it works for me:

  1. open command prompt with admin rights
  2. yarn global add ndb
  3. cd to project folder
  4. yarn rebuild
  5. ndb node ./dist/src/index.js (path will depend on project!)
  6. if you need to set break point before project starts open ndb with command ndb ., browse to sources and put break point, then follow #5.
Was this page helpful?
0 / 5 - 0 ratings