20

I am running a Node.js application within a Linux VM in Oracle VirtualBox. The port 5858 on the VM is forwarded to the port 5858 on the localhost.

I am running WebStorm on the host operating system (Windows 7). In WebStorm I click Run >> Edit Configurations.... I then click the add button (the plus sign) and select Node.js remote debug. I then enter the name of the configuration and leave the host as 127.0.0.1 and the port as 5858. I click OK, and ensure my node app is running on the VM with the --debug flag.

I then click the bug icon in the WebStorm toolbar and the debugger appears at the bottom of the WebStorm IDE.

How can I confirm that the connection is made successfully? Breakpoints do not work and the console in WebStorm remains empty (while information is sent to the console on the Linux VM). Can anyone suggest what remains to be done to get the remote debugging session working?

Damian Krawczyk
  • 2,241
  • 1
  • 18
  • 26
Ben Aston
  • 53,718
  • 65
  • 205
  • 331
  • If anybody happen to run WebStorm 2017.2 and nodejs with --inspect flag make sure you update WebStorm to 2017.3 EAP first which comes with support for the new Chromium debug protocol that is enforced by the --inspect flag. – WooDzu Nov 25 '17 at 07:59

4 Answers4

21

Instead of just

$ vagrant ssh

try

$ vagrant ssh -- -L 5858:127.0.0.1:5858

I also removed the line config.vm.network :forwarded_port, guest: 5858, host: 5858 from my Vagrantfile and did a $ vagrant reload.

In your vagrant box run

$ node --debug-brk path/to/file.js

Set up a Node.js Remote Debug Run/Debug configuration, defaults are fine (host 127.0.0.1 and port 5858).

Run the following from your HOST machine, not your vagrant box.

$ telnet 127.0.0.1 5858

You should get a response like

Type: connect
V8-Version: 3.14.5.9
Protocol-Version: 1
Embedding-Host: node v0.10.28
Content-Length: 0

If you don't then something wrong with the port forward. You could try a different port on the host end and update your webstorm Run/Debug configuration that was previously set up from port 5858 to the new port.

Now run the NodeJS debug configuration. It should immediately pick up the file and you'll be able to debug it.

I based my steps off of PHPStorm, but it should be the same. I'm assuming it'll work on all IntelliJ-based IDEs as long as you have the NodeJS plugin installed.

2upmedia
  • 2,832
  • 1
  • 20
  • 16
16

1) From WebStorm manual:

When the application to debug is running on a physically remote host, you need to run a proxy or any other software that ensure port forwarding on the Node.js server. This is necessary because the debug port can open only on the localhost network interface. The localhost network interface cannot be accessed from another machine therefore WebStorm cannot connect to it upon initiating a debugging session.

2) localhost on VM is not the same localhost (as strange as it sounds) on host.

3) V8 debugger is listening on localhost

Solution:

You can setup ssh proxy to linux VM: ssh -L 5858:127.0.0.1:5858 <vm_user>@<vm_ip> -N and here you can find answer how to do it on Windows -> stackoverflow If you will do the above your debug configuration in WebStorm will be Host: 127.0.0.1 Port: 5858

You can check if WebStorm is connected to remote debug session by checking Debugger tab and Variables panel. can not connect, connected

Community
  • 1
  • 1
Damian Krawczyk
  • 2,241
  • 1
  • 18
  • 26
  • There is a line in the vagrant file used for creating the VM `config.vm.network :forwarded_port, guest: 5858, host: 5858` this should do the trick right? WebStorm currently remains on `Connecting to 127.0.0.:5858` in the Debugger variables panel. – Ben Aston Feb 10 '14 at 13:57
  • 1
    vagrant does not allow to forward ports binded to lo. It can forward of service is using 0.0.0.0 – Damian Krawczyk Feb 10 '14 at 17:44
  • Please clarify your last comment - I do not understand. – Ben Aston Feb 12 '14 at 16:00
  • Node.js as example - http.createServer(...).listen(5858); is listening on 0.0.0.0:5858 which means that no matter to what IP attached to your machine (loclahost, eth0 ip ...) request will came node will pick it up but if you will make it listen to lo only - http.createServer(...).listen(5858, '127.0.0.1'); you won't be able to access it via external ip addresses (attached to eth0, eth1, ...). Vagrant is not able to forward ports from your lo (127.0.0.1) to make them available from outside and V8 debugger is listening on 127.0.0.1 by default. Ssh command I've provided is creating tunnel that... – Damian Krawczyk Feb 12 '14 at 17:34
  • ... will forward all traffic from host machine on port 5858 to localhost:5858 on Vagrant VM. – Damian Krawczyk Feb 12 '14 at 17:35
  • As far as I can tell this discussion has not resolved my issue. I want to attach the debugger in Webstorm to a node application running in an VirtualBox VM. I am still unable to do this. I am happy to award the bounty regardless due to effort. I know that ports on the VM can be forwarded successfully to the host (configured using Vagrant) because I am already doing so for port 5858 (among others). – Ben Aston Feb 17 '14 at 10:57
5

This is what I do in my linux Debian VM:

install balancer

sudo apt-get install balance -y

create a route in balancer to reroute your 5858 port to 5859

balance 5859 127.0.0.1:5858

start your app

node --debug app.js

now you can access it from outside the VM on port 5859

nicco82
  • 179
  • 2
  • 9
2

Just a heads up, when I tried this I had to use --debug-brk. Running with just --debug didn't allow WebStorm to set breakpoints after connecting.

Luciano
  • 1,119
  • 12
  • 18