I've got Ubuntu in WSL 2 running on Windows 10 (Version 10.0.19044 Build 19044) host OS.
Windows Defender Firewall is fully disabled.
What does work
A very basic Express app
index.js
:const express = require('express') const app = express() const port = 3000 app.get('/', (req, res) => { res.send('Hello World!') }) app.listen(port, () => { console.log(`Example app listening on port ${port}`) })
Inside WSL, I run
npm init
,npm i express
and finallynode index.js
.Then in a browser on the host OS I visit http://localhost:3000 and I see "Hello World!".
Other Node apps such as EmberJS:
npx ember-cli -- new foo cd foo/ npm start
This starts a dev server on port 4200 inside WSL.
Then in a browser on the host OS I visit http://localhost:4200 and I see a boilerplate app.
What does not work
(of course, I make sure first that ports are available)
Running a Node
http-server
:I start
npx http-server -p 3000
inside WSL.I access http://localhost:3000 in the host OS. Despite using the same port and host as before, I see "Connection refused".
From inside WSL, the page does open (e. g. with
links http://localhost:3000
).Running a Python HTTP server:
I start
python3 -m http.server 4200
inside WSL.I access http://localhost:4200 in the host OS. Despite using the same port and host as before, I see "Connection refused".
From inside WSL, the page does open (e. g. with
links http://localhost:4200
).Running headless Chrome with
--remote-debugging-port
.Chrome on the host OS is unable to connect to the headless Chrome in WSL.
What I tried
Neither of these helps:
- Rebooting.
wsl --shutdown
.- Googling. A lot.
Notes
When I start e. g. npx http-server -p 4200
, an entry appears in the Network tab of the Resource Moninor in the host OS. It says port 4200
, process wslhost.exe
, firewall status "Allowed, not restricted".
Yet, I see this:
UPD 1: I've been 🤯 over it for days, and I see individual attempts pass. Like once I've seen Chrome Inspector connect to the headless Chrome in WSL, twice I've seen http-server
serve an index page. It hasn't worked for dozens of attempts, then it suddenly works once, then it stops working again.
At the same time, node index.js
with Express and ember serve
are 100% reliable and are always accessible from the host OS, even on the same port.
UPD 2: the problem does not happen with IPv6. Adding -a ::
to http-server
lets me connect reliably over IPv6. So why is IPv4 failing?!
Please help.
localhost
(or the IPv4/v6 equivalent) for each connection, I believe? What happens if you try the WSL2 IP address? That is, the one found withip addr show eth0
? Remember to bind the service to0.0.0.0
if you do that, but I'm wondering if using the IP vs.localhost
will have different results.