17

Vite + Reactjs server is running but I am getting "This localhost page can’t be found No webpage was found for the web address: https://localhost:4200/" on the browser

Muhammad Raheel
  • 617
  • 1
  • 6
  • 15
  • Does this answer your question? [Vitejs React This localhost page can’t be found](https://stackoverflow.com/questions/70681314/vitejs-react-this-localhost-page-can-t-be-found) – ggorlen Jan 08 '23 at 21:29

19 Answers19

24

A common mistake when moving from create react app/react-scripts to Vite is having the index.html in the public folder. Make sure your index.html file is in the root directory for your Vite project.

sandrooco
  • 8,016
  • 9
  • 48
  • 86
omalk98
  • 241
  • 1
  • 3
  • 6
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 13 '22 at 03:58
9

Just in case someone is looking for a solution and the other answers didn't work, here is what worked for me.

In vite.config.js I had to set the host to '127.0.0.1' in the server options.

{
  server: {
    host: '127.0.0.1'
  }
}

The default value is localhost but why that doesn't work I still don't know...

Darwin Island
  • 141
  • 1
  • 3
  • I encountered this issue when using VSCode with the Remote-SSH extension for my Astro project. It worked fine if I logged into the remote box, but to access the dev server remotely I had to explicitly set host to '127.0.0.1'. Apparently this is an issue with Vite-based projects and VSCode Remote-SSH: https://github.com/microsoft/vscode-remote-release/issues/8213 – Brandon Mitchell Jul 04 '23 at 15:37
  • I encountered this issue after upgrading node version, Vite app couldn't be accessible via localhost though CRA app worked. This solution worked for me like a charm. Thanks a lot! – iconique Aug 03 '23 at 16:52
7

TLDR: Change the Port

For me, it was that I already had something running on the port that Vite was trying to serve the app on (3000).

Doing a quick lsof -nPi :3000 (MacOSX) I saw that my docker desktop was running on that port.


You can do this statically by updating a server key in the vite config (vite.config.js)

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: 8000
  }
})

Or dynamically via the start command: vite --port 8000


https://vitejs.dev/config/#config-file

orpheus
  • 1,060
  • 2
  • 15
  • 23
4

I had the same problem and it was related to the project folder name. It had some spaces in the name of the project's folder. I removed spaces and the node_module folder then I installed packages again, and now it's working fine.

Mehdi Nasiri
  • 65
  • 2
  • 6
  • yes, I have the same problem, I also just removed spaces from the folder name, and its works for me. – Muhammad Raheel May 19 '22 at 13:03
  • yep! I had cloned a repo from azure and the root directory had some special characters. After renaming the root folder name to a simple name, it started working fine. – Aashay Amballi Dec 13 '22 at 12:01
3

I have the same issue, but there was nothing using the port 3000, but anyways, I change it to port 8000 and it's working

export default defineConfig({
  plugins: [react() tsconfigPaths()],
  server: {
    port: 8000
  }
});
ggorlen
  • 44,755
  • 7
  • 76
  • 106
2

I got the same problem. click collapse your project structure in vscode, vite config file was not at the level of index.html. So make sure it should be at where index.html is, Double check please!!

  • This is the same answer as [this](https://stackoverflow.com/a/71096224/6243352). Maybe just upvote that rather than adding a new answer. – ggorlen Jan 08 '23 at 21:30
1

My issue that happened to me that seems possible to commonly happen was I had another index.html file in my public dir. That file will take precedence over the one in your project root, so make sure you delete that or move it elsewhere. For me, it was a remnant of a vite build that I didn't notice hung around

Jay
  • 998
  • 1
  • 10
  • 22
1

Create a vite.js project and then edit the vite.config.js like this (it helps me to run the project on localhost):

import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    host: "127.0.0.1",
    port: 3000,
  },
});
1

You don't have to change any port or anything just a --host flag should do the work.

yarn dev --host

Make sure your firewall is disabled on the private (and sometimes domain also) network, and nodejs runtime is allowed in the firewall allowed apps.

first
  • 616
  • 1
  • 7
  • 13
0

None of the answers here helped me. It turns out uBlock Origin was preventing the index.html page from loading when accessing it through localhost:3000. Disabling it or trying it in incognito mode works!

Hamed
  • 23
  • 1
  • 6
0

For me, beside server host true in the vitejs settings, I needed to update the docker run command with "--service-ports" (https://docs.docker.com/engine/reference/commandline/compose_run/):

docker-compose -p myservice run --service-ports --rm node npm run dev
Alin Pop
  • 317
  • 2
  • 8
0

I guess that you have a problem in your directory, make sure that the index.html is global, means not in any directory besides your project, and also make sure that the vite.config.js is on the same level as the index.html.

Yahia
  • 31
  • 4
0

In your vite.config.js file you can specify the value of root to direct to your index.html file. For example if your structure is src/public/index.html', you would simply need to set root: 'src/public'

0

Probably you will be using

vite start, or

vite run start

Instead of these please run this:

npm run dev

or if you using yarn then

yarn run dev

This resolved my issue.

Thank you

ShoaibShebi
  • 59
  • 1
  • 5
0

You Just Have to cd into the src folder and then run npm run dev

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 25 '23 at 21:11
0

Just in case you don't have any of the issues the people above mention, it is also possible it will not run if you are working in a directory that has a space in its name like My%20Project%20 this will break vite server.

See this open issue - https://github.com/vitejs/vite/issues/8904

Komsomol
  • 698
  • 10
  • 27
0

I know this is old to answer, but I fixed it using the command below.

Use:

npm run dev -- --host

Instead of:

npm run dev --host

Just add the double hyphen.

Blues Clues
  • 1,694
  • 3
  • 31
  • 72
0

I think the vite localhost at default starts at localhost:5173, try this and see if it works

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 03 '23 at 14:07
0
  server: {
    allowedHosts: [".localhost"],
    host: true,
  },
Synchro
  • 1,105
  • 3
  • 14
  • 44