Debugging a node app hosted on a VM using Visual Studio Code

I have verified that tunnelling the port 5858 via ssh works in so far that you can connect to node running inside the VM and use debugger functionality that does not involve source paths (Source paths are used for breakpoints and step events, etc.) The problem with source paths is that VSCode needs access to the source files with the same paths as node running inside the VM. Even if you share the source through samba, the absolute path leading to a file might be different between inside of the VM and outside. The only workaround for VSCode Preview is to make the paths identical e.g. by introducing (symbolic) links etc. I have create a bug on our side to improve the source path matching.

Andre Weinand, Visual Studio Code


My Vagrantfile has the following mapping from my host to an Ubuntu VM:

config.vm.synced_folder "C:/Users/me/Documents/app", "/home/app"

I got node debugging in VS Code working by doing this:

  1. Forward port 5858 in the VM: config.vm.network :forwarded_port, host: 5858, guest: 5858

  2. In VS Code, set the following launch.json:

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Attach",
                "type": "node",
                "request": "attach",
                "port": 5858,
                "address": "localhost",
                "restart": false,
                "sourceMaps": false,
                "localRoot": "${workspaceRoot}/api",
                "remoteRoot": "/home/app/api"
            }
        ]
    }
    
  3. In the VM: cd /home/app/api

  4. Run node --inspect=0.0.0.0:5858 server.js

  5. In VS Code, open folder C:/Users/me/Documents/app, set breakpoints and press F5.

If you can telnet to port 5858 and get the same response both from inside and outside the VM, it probably means that the mapping of files is wrong.