9

I'm looking for a little help in debugging my app using Visual Studio Code, where my app is held in a virtual machine hosted by Oracle Virtual Box.

The VM has been provisioned with a typical setup of node, express, node-inspector etc. and I am able to debug my app using node-inspector (i.e. the port for node-inspector has been forwarded within the vm and if I set my app running with "node --debug-brk app.js" it listens on port 5858 and I can navigate to localhost:8080/debug?port=5858 to start debugging).

However in VSC if I use the "attach" option in debug I cannot get to a breakpoint at all.

Is there something special that I am missing here or are there any log files I can look at - I'm on OSX Yosemite and the VM OS is running in virtual box is a headless OpenSuse, provisioned with vagrant?

NB: I have tried telnetting to the VM on port 5858 and I get a different response from within the VM to the local machine itself, as indicated below:

Inside the VM:

telnet 127.0.0.1 5858
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
Type: connect
V8-Version: 3.14.5.9
Protocol-Version: 1

Embedding-Host: node v0.10.32 Content-Length: 0

Outside the VM:

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

Any help would be much appreciated?


Yes I'm running VSCode outside the VM - The VM is headless.

The ports are already configured to be forwarded inside the vagrant provisioning script. However a colleague has informed me that there might be a company policy that is being forced upon us relating to our network adapters - preventing/causing my connection issue.

However I've tried a different approach. I've tunnelled a connection via ssh for all the traffic on port 5858:

ssh -i myprivatekey -L 5858:localhost:5858 tempuserlocalhost -p 2222 

Now when I start debugging the app (i.e. node --debug app.js) and use the attach option the debugger clearly attaches. It doesn't hit my breakpoint in app.js though, when it should do.

In actual fact if I pause the debugger I get a list of the local variables and a call stack but the following error is shown:

Error opening 'app.js' (File not found)

Note: app.js and the other code files are not held on the VM, they are held on my local machine with samba shares configured accordingly. Perhaps this is causing the confusion?

4 Answers 4

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

In step 4. you only need either '--debug' or '--debug-brk' but not both. For a server process '--debug' is probably sufficient if you do not want to debug the startup sequence of the server.
In Version 1.11.2 outDir is deprecated. I just removed it and it all works. Thanks!
5

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

1 Comment

Please consult the section "Remote Debugging Node.js" in code.visualstudio.com/docs/editor/debugging for an update.
2

I guess you're running VSCode outside the VM, so similar to your telnet from outside the VM, VSCode will not be able to attach to the port since it doesn't detect the v8 debugging protocol.

I think you need to somehow configure your VM hypervisor to map the 5858 port from the VM to the VM host.

You will know you've done it correctly if telnet from outside the VM will output the same as telnet from within the VM.

1 Comment

Thanks for the quick reply Alex. I've updated the question to include further progress I have made.
0

its not so complicated just share your file folder by Virtualbox just share the folder in the both sides outside and inside VM you can mount your folder to be permanent or temporal in both OS's in my case i use windows 10 to user full visual studio functions but it can be done with linux or OSx just do some bridge stuff in the NIC (network card) TIP: i user bitnami is more clear that unknown vagrant repositories

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.