WSL: 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.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 32
- Comments: 80 (3 by maintainers)
Restarting WSL fixed this issue for me, don’t have any evidence but I believe it has to do with the initial wsl boot on system startup.
My fix in powershell.
Restart-Service LxssManager - fix for me, honestly this need to be top priority
Thanks @fredyfx
Using the IP address does work, but it’s localhost that I need/want, mainly due to how the app works.
wsl --shutdown wsl
works.
Looks like disabling
Turn on fast startup (recommended)inControl Panel -> Power Options -> Choose what closing the lid doesfixes the issue.From this issue #4636
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
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
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
logs.zip
None of the suggestions (restarting WSL, restarting LxssManager, etc.) have worked for me this morning after a fresh reboot of my system.
This is not a useful dev environment if things like this keep happening, folks. We need some real understanding/solutions to why localhost breaks, intermittently). Losing lots of dev time to trying to resolve this issue.
Using a different port worked for me
PORT=4321 yarn startAre you all using docker in WSL2 at the same time, I do, and that’s probably the cause of the problem … This can help meanwhile: https://abdus.dev/posts/fixing-wsl2-localhost-access-issue/
I started having this issue only on port 3000 (for a react app) after I ran a netsh command to expose a port externally and then later restarted my computer. This is what I originally ran along with setting up a firewall rule to open up 3000.
To fix it I had to explicitly add another portproxy, where 172.29.222.172 is my current WSL address. Of course, I’ll have to repeat this on my next wsl restart, which isn’t ideal.
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 9999and in windows 10ncat 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 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 9999And, when sending: From WSL - nc localhost 9999 always works. From Windows 10,
ncat localhost 9999sometimes works - and, if it works once, it never seems to stop working as long as I don’t suspend or power cycle my system. Whenncat localhost 9999fails (and anncat -zv localhost 9999shows the port isn’t respond), thenncat wslipaddress 9999always 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.)
This worked for me too 😃
I’m having the issue on a desktop so I’m sure it isn’t the lid closing that’s the problem
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?
Thanks so much @pmartincic .
I downgraded from WSL 1.1.0 to 1.0.3, per @rudyzeinoun’s comment on #9508 using the following commands and now everything is working:
I’m having the same issues problem started 3 days ago aswell has anyone found a find for this
Started happening again after last update. Tried pretty much all advices in this thread (disabled fastboot, installed sandbox / hypervisor, restart wsl daemon, etc, etc) - none of it helps anymore. Port 3000 is just not reachable via
localhost. Work fine via IP though 🙃I really hope that the guys will fix it, because I have to use different operating systems to work.
Have you tried to use the IP of your computer instead of localhost? I saw before something related.
I solved the issue of my previous comment by executing the following (again, in an Administrator-level Powershell prompt):
The previous commands failed because
$Packagewas set to multiple packages. Why multiple versions of WSL2 ended up being installed (1.1.0.0and1.1.2.0) is, from my perspective, another highly disruptive WSL bug.Please, WSL team, escalate this issue.
@ddaversa as mentioned https://github.com/microsoft/WSL/issues/4885#issuecomment-1400810108 this is a bug for pre 1.1.0, please look at https://github.com/microsoft/WSL/issues/9508 for >= 1.1.0
What version are you using? The issue on 1.1.0 is being addressed.
I am having the same problem seemingly out of nowhere today. after a reboot,
npm run starton a create-react-app project doesnt work for localhost:3000 or any port (says connection refused), but using the ip address http://172.20.242.90:3000 works. running anything else works and I can access thru windows, for examplenc -lk 5000and evennc -lk 3000, I feel like that rules out firewall etc. also Ive noticed if I runnpm run startand let it run, I cant connect thru windows, I can curl it from inside the wsl. but if I then runnc -lk 3000on the same port, nc fails as expected because the port is in use, but now I can access my react app thru localhost:3000 in chrome in windows. but then closing thenpm run startreact and rerunning, back to square 1 with it not working.This was the fix for me, I had forgotten I added a portproxy and was wondering why all of a sudden my 3000 port wasn’t working but all others were… after removing the portproxy I was back in business, thank you for this reminder 😄
Shutting down the WSL from powershell with command
wsl --shutdownfixed the issue.Worked for me
I am having the opposite problem. localhost:3000 works, but the LAN IP address does not work for other devices. https://stackoverflow.com/questions/64892293/create-react-app-on-your-network-with-wsl2-not-working
I’m having the same experience as vadimjoy, Intermittent problem. Restarting lxmssmanager works - but in my opinion not a long term solution.
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.