Cannot use localhost to access a react-app running in WSL2
Microsoft Windows [Version 10.0.19551.1005]
Steps to reproduce the problem:
- Execute
npm startto launch a react app in WSL2. - Open http://localhost:3000 in a browser in Windows.
- What's wrong / what should be happening instead: Browser cannot open the site / the website should open.
I know this is an existing issue, and that there are other bug reports filed (with no apparent solutions), and I know that I'm running a pre-stable setup, so I'm not moaning.
I've recently switched to the slow lane.
That said, I simply can't use WSL2 for developing anything without access to localhost from a browser, and using an ip address that changes regularly is not a valid workaround. There are just too many APIs that use callbacks, redirects, scope, etc. that to code around this issue is too much effort.
At present, the only suitable, yet non-ideal, workaround is to not use WSL at all, clone my repo into Windows, and run everything in powershell.
Have you tried to use the IP of your computer instead of localhost? I saw before something related.
Since you're on 19551 this should work. Maybe your firewall blocks port 3000? Also check if your app is really configured to listen on port 3000?
Thanks @fredyfx
Using the IP address does work, but it's localhost that I need/want, mainly due to how the app works.
Thanks @ad-on-is
Ya know, if I weren't a complete idiot, I would have thought about setting the firewall to allow port 3000! But I am so, I didn't.
Note: I need to say that over the past few months, with various Windows Insider releases, this has worked and not worked intermittently.
So, I created the firewall rule, before checking right before doing this. And, voila, it worked.
Then, as a sanity check, I disabled the firewall rule, and tried again. And it still works.
So, I started starting apps on varying ports (3001, 8000, 9999) and accessing them w/o any firewall rules, and they worked.
I checked and no updates were applied since I posted this initial message.
The one thing that is different today, is that I have no Visual Studio Code remote sessions running, because I'm doing all the coding in windows, not in WSL.
So I launched a remote coding session in VScode and tried to access the site, and it worked.
Per the note above, in that this is an intermittent problem, I'm going to leave this open and check the behavior periodically for a week, reporting in if I re-discover the issue, and closing it it is actually gone.
I have an ongoing challenge with localhost not forwarding from Windows 10 (multiple versions, but currently 19041.113) to my WSL instance.
A simple test
WSL: nc -lk 9999
And, when sending:
From WSL - nc localhost 9999 always works.
From Windows 10, ncat localhost 9999 sometimes works - and, if it works once, it never seems to stop working as long as I don't suspend or power cycle my system.
When ncat localhost 9999 fails (and an ncat -zv localhost 9999 shows the port isn't respond), then ncat wslipaddress 9999 always works - so, for whatever reason, localhost just decides not to forward to my wsl instance.
I'm only ever running a single WSL instance. It sometimes occurs after a power cycle.
The change in state between forward/not forward isn't associated with any firewall rules (I've disabled all of them) or config changes on WSL (I haven't touched them).
I'm kind of stuck on creating an thoroughly repeatable case, but I'd be happy to dig in and provide further diagnostics if someone can think of something for me to try when it occurs, as it appears to do on a semi-periodic basis (I run Jupyter-Labs against my WSL instance, so I get lots of opportunity to see this happen.)
Update - system was just updated to 19041.153. I accepted the restart, and upon starting WSL got the request to update through https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel which I did.
Came into WSL, and tried nc -lk 9999 and in windows 10 ncat localhost 9999 - this failed. Immediately tried .\ncat.exe 172.31.141.125 9999 (the IP of the WSL instance) - and this worked immediately - tried localhost again, and that failed.
Tried a wsl --shutdown - that did not work. So, my localhost forwarding is now wedged again. I'll see what sequence of activity I take that results in it working again.
I've had several reboots - and so far Build 19041.172 hasn't stopped forwarding. Fingers crossed that something was changed that resolved this. I'll keep rebooting and banging on it and see if it happens again.
I have a same problem.
default via 172.29.64.1 dev eth0
172.29.64.0/20 dev eth0 proto kernel scope link src 172.29.71.101
I'm using the nodejs API gateway on port 6565 If I use 192.29.71.101, then I can access it, but it doesn’t work through the localhost Build 19041.172
I am using WSL2 too, I tried to install postgresql in my WSL, I cannot access it using TablePlus if I am using localhost, only works if I use ip address.
OS Build: 19041.207
From this issue #4636
- Try to enable Windows Sandbox & Windows Hypervisor on Windows features
- Restarting LxssManager from windows services worked for me. I hope it keeps on working 🤞
Step 2 must be performed after each computer shutdown. Then the localhost becomes available again. I'm not shure about first step, but turned on the sandbox just in case. Build 19041.207
From this issue #4636
- Try to enable Windows Sandbox & Windows Hypervisor on Windows features
- Restarting LxssManager from windows services worked for me. I hope it keeps on working 🤞
Step 2 must be performed after each computer shutdown. Then the localhost becomes available again. I'm not shure about first step, but turned on the sandbox just in case. Build 19041.207
I'm having the same experience as vadimjoy, Intermittent problem. Restarting lxmssmanager works - but in my opinion not a long term solution.
I'm having the same experience as vadimjoy, Intermittent problem. Surely we aren't expected to continually restart LxssManager for the rest of our lives?
I really hope that the guys will fix it, because I have to use different operating systems to work.
My temp solution using powershell and Task Scheduler:
- Allow scripting using powershell (for example: Set-ExecutionPolicy RemoteSigned)
- Create a file RestartLxssManager.ps1 with the contents: Restart-Service -DisplayName LxssManager
- Add script execution at system startup trigger in the Task Scheduler with highest available (command: powershell.exe, arguments: -File C:\RestartLxssManager.ps1).
Exported task (replace with your YOUR_DESKTOP_NAME\user, YOUR_USER_ID):
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<RegistrationInfo>
<Date>2020-04-17T16:31:09.8645471</Date>
<Author>YOUR_DESKTOP_NAME\user</Author>
<URI>\RestartLxssManager</URI>
</RegistrationInfo>
<Triggers>
<LogonTrigger>
<Enabled>true</Enabled>
<UserId>YOUR_DESKTOP_NAME\user</UserId>
</LogonTrigger>
</Triggers>
<Principals>
<Principal id="Author">
<UserId>YOUR_USER_ID</UserId>
<LogonType>InteractiveToken</LogonType>
<RunLevel>HighestAvailable</RunLevel>
</Principal>
</Principals>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>false</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>true</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>powershell.exe</Command>
<Arguments>-File C:\RestartLxssManager.ps1</Arguments>
</Exec>
</Actions>
</Task>
This should be resolved, you should be able to access it using localhost. We haven't been able to repro this on the team.
Can you provide us with a project that we can repro this on? Or can you take logs when you have this problem and submit them here?
This should be resolved, you should be able to access it using localhost. We haven't been able to repro this on the team.
Can you provide us with a project that we can repro this on? Or can you take logs when you have this problem and submit them here?
Hi Craig,
Thanks very much for taking the time to look at this. I appreciate you are a busy gu.
I'm on build 19608.1000 and have just fresh re-installed Ubuntu-18.04 WSL2 distro.
Still happening intermittently - seemingly with any lite server I'm using for dev... angular ng serve etc,
I just now freshly installed lite-server in the WSL2 18.04 distro and it is also affected. I can't see localhost:3000 but can see 172.28.137.63:3000 in my windows browsers.
The docker WSL2 containers aren't affected - I can still see my Hasura container (for example) at localhost:8080.
wsl -t <DistroName> doesn't fix the problem
Restart-Service LxssManager DOES fixt the problem
Happy to provide logs - which logs would you like?
Tim.
Hi Craig,
Here are logs from Saturday (couldn't reproduce error) and this morning (error is happening right now). Screenshot also attached.
Any help from anyone greatly appreciated.
Tim

I've just upgraded to build 19613.rs_prerelease.200417-1557.... issue persists
Also now upgraded to ubuntu 20.04 from the microsoft store.... issue persists.
I'm also having this issue, 19613.1000 and running Pengwin. Here are my logs wsl_logs.zip
Interesting additional finding. When it occurs it is ports <= 4518 that are affected. WSL2 Ubuntu services listening on ports >= 4519 are not affected.
screen shot below

Does anyone having this issue have a network set up on the host windows 10 machine that is as straightforward as a laptop with one wifi card?
I'm looking for reasons why this happens to me on both my desktop and xps 13... Even with fresh install on win 10.
Both machines have a sonicwall VPN app on them (issue doesn't seen to correlate with the VPN being connected).
The desktop has both a wired and wireless NIC both connected to the same network but with different dhcp allocated IP addresses.