reactjsubuntulocalhostvitewindows-subsystem-for-linux

React application doesn't run on localhost:8080 inside WSL


I am using WSL2 (Ubuntu) to develop a React application built using Vite. When running the npm run dev command, the terminal reports that the server is running on localhost:8080, but:

  1. When trying to open http://localhost:8080 everything works in the Windows browser.
  2. However, inside WSL, for example, when executing curl http://localhost:8080 or netstat -nltp, it can be seen that port 8080 is not listening.
  3. Also, checking through lsof -i :8080 does not show any active processes.
  4. Third-party tools (netstat, lsof) do not detect that Vite is listening on port 8080.

My goal is to ensure that port 8080 is actually listened to and accessible inside WSL2 (for example, via curl http://localhost:8080 ).


Solution

  • Run Vite with --host 0.0.0.0 to make it listen on all network interfaces:

    vite --port 8080 --host 0.0.0.0
    

    Or in package.json:

    "scripts": {
      "dev": "vite --port 8080 --host 0.0.0.0"
    }
    

    Then, inside WSL2, get your Windows host IP:

    ip route | grep default 
    

    You’ll see something like default via 172.27.208.1. Use that IP:

    curl http://172.27.208.1:8080 
    

    Now it works from inside WSL2!