Search code examples
node.jsdockervisual-studio-codevscode-debuggernode-inspector

VSCode debugger refuses to attach to node process with no error or logs?


We're using node v18.3.0

I'm starting my nestjs app, inside a docker container, with this command nest start -e \"node --inspect-brk\". Which gives the following output:

node@bfcdb32e737f:/opt/thallo/exchange-be$ npm run test:debug:wait

> [email protected] test:debug:wait
> nest start -e "node --inspect-brk"

Debugger listening on ws://127.0.0.1:9229/c4d73a54-6165-4502-bcea-1fa4390db95c
For help, see: https://nodejs.org/en/docs/inspector

So it looks like it's waiting for a debugger to attach - excellent!

I have forwarded port 9229 on the docker container, docker ps:

CONTAINER ID   IMAGE                           COMMAND                  CREATED         STATUS         PORTS                                                                                  NAMES
bfcdb32e737f   exchange-be_exchange-be-uiapi   "tail -f /dev/null"      7 minutes ago   Up 7 minutes   0.0.0.0:3001->3001/tcp, :::3001->3001/tcp, 0.0.0.0:9229->9229/tcp, :::9229->9229/tcp   exchange-be-uiapi

And I can telnet to that port from the host machine telnet localhost 9229:

Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

This is my launch.json:

{
  // Use IntelliSense to learn about possible attributes.
  // Hover to view descriptions of existing attributes.
  // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
  "version": "0.2.0",
  "configurations": [
    {
      "name": "Attach Exchange BE",
      "port": 9229,
      "request": "attach",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "type": "node",
      "sourceMaps": true,
      "cwd": "${workspaceRoot}",
      "restart": true
    }
  ]
}

But when I launch the debugger I just get this little progress bar for a few seconds and then the debugger stops: enter image description here

But there's no error log or anything so I have no idea how to fix this. Any help is greatly appreciated!


Solution

  • For debugging a NodeJS application running a docker 127.0.0.1 interface wont reach the docker network try using 0.0.0.0.

    In the debug command at the package json change it to nest start -e \"node --inspect-brk=0.0.0.0:9229\"

    The 0.0.0.0 will tell the debugger to look at all available local network interfaces.

    In the vscode debug configuration "address": "0.0.0.0" and it is recommended to use "remoteRoot" and "localRoot" directives for syncing the position of the current vscode work directory files to the files in the docker file system.