DEV Community

Alex Antsiferov
Alex Antsiferov

Posted on • Updated on

Troubleshooting WSL + localhost:3000 'connection refused' issue

Just spent an hour trying to figure out why the browser wouldn't open localhost:3000 (served by Webpack/create-react-app in WSL2).

Normally, a server running in WSL should be accessible from the Windows (host) machine without any issues.

My troubleshooting steps, roughly:

  • check /windows/system32/drivers/etc/hosts file --nothing special here...
  • check Windows firewall rules, just in case
  • shut down WSL VM with wsl --shutdown command and restart it
  • run another app in WSL using a different port -- hmm, this works...
  • change the port of the app in question in Webpack config: PORT=3006 react-scripts start -- also works... so the problem is in the specific port!
  • see if some process is holding the port 3000 in Linux: ss -tulpn
  • check if a process is holding the port in Windows: netstat -ano | findstr :3000 or Resource Monitor -> Network tab -> Listening ports -- aha! there is one!
  • find the process name by PID in the Task Manager -> Services

Turns out, the port was taken by IP Helper service (iphlpsvc).

I could not find a way to reconfigure it, and some quick research showed that it wasn't a critical service (at least in my case), so I just disabled it in the Services.

Top comments (9)

Collapse
 
royalrex profile image
Sebbe Selvig

I found that changing the port helps, each time I shutdown my webpack-dev-server and my laptop, the next day the port is "dead" again.

Using wsl --shutdown works, so starting my day with that command solved any issues, quite annoying.

The port issue is not a problem for other appliances as docker containers.

Collapse
 
ciprian_42 profile image
Ciprian

I signed up to dev.to only to like this comment :)

I was tired of restarting my PC to get Nuxtjs working and couldn't find any other fix to this bug.

You Sir have saved my life, haha.

Collapse
 
acornwall profile image
Alexandria Cornwall

Just a quick heads-up: Disabling iphlpsvc can break TCP forwarding from WSL 2. You might use this feature, for example, if you're trying to debug locally from your phone: github.com/microsoft/WSL/issues/4150

I don't really have a solution other than disabling iphlpsvc, starting up node / yarn, then re-enabling iphlpsvc (or just choosing a different port to start on).

Collapse
 
maxim_mazurok profile image
Maxim Mazurok

This command causes the port to get hogged by IP Helper:

iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
Enter fullscreen mode Exit fullscreen mode

And this command can un-hog it:

$addr='0.0.0.0';
$port=3000;
iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
Enter fullscreen mode Exit fullscreen mode

But you do loose port forwarding from WSL to Windows in this case.

Also, in my case I was able to create server on the same port, and Chrome could open site ok, but Chrome controlled by Cypress coudln't open it, was timing out. Which is weird.

Collapse
 
wetterkrank profile image
Alex Antsiferov • Edited

@acornwall Thanks for mentioning this! Indeed, IP Helper service may be needed at some point, so it's definitely not the bulletproof solution :)

Collapse
 
mersadajan profile image
Mersad Ajanovic

About time you get a mac :D

Collapse
 
wetterkrank profile image
Alex Antsiferov

Or just switch to Ubuntu completely :)

Collapse
 
mersadajan profile image
Mersad Ajanovic

Well, I wouldn't mind Ubuntu if it weren't for the lack of a proper ecosystem and added os maintenance. I used windows and ubuntu for years but it just takes too much time to care for the system. Most of the time I found myself with one issue or another that is not related to actual work. On mac, I had more peace of mind and focus on work.

Thread Thread
 
perseon profile image
perseon

linux needs the least maintenance of the 3 OSes. I use it on my workstation and it never got in my way. macos is just an interface that gets in the way of proper programmers.