Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 510
Nicholas Ford Avatar asked May 08 '15 09:05

Nicholas Ford


People also ask

How do you debug node in visual code?

There are a few ways you can debug your Node.js programs in VS Code: Use auto attach to debug processes you run in VS Code's integrated terminal. Use the JavaScript debug terminal, similar to using the integrated terminal. Use a launch config to start your program, or attach to a process launched outside of VS Code.

How do you debug an app in VS Code?

To run or debug a simple app in VS Code, select Run and Debug on the Debug start view or press F5 and VS Code will try to run your currently active file. However, for most debugging scenarios, creating a launch configuration file is beneficial because it allows you to configure and save debugging setup details.

How do I debug a console application in Visual Studio?

Press F5 to run the program in Debug mode. Another way to start debugging is by choosing Debug > Start Debugging from the menu. Enter a string in the console window when the program prompts for a name, and then press Enter . Program execution stops when it reaches the breakpoint and before the Console.


1 Answers

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

like image 179
Andre Weinand Avatar answered Sep 21 '22 17:09

Andre Weinand