Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why running index.html won't work for a `create-react-app` build?

I am new to create-react-app. I want to know why opening the index.html form a build does not work ?? I know to serve a build serve -s build should be used, but I want to know why a build react app can’t be started without serving.

I will explain further: What I do is...

  1. create-react-app helloworld
  2. Make some code changes and verify that app is running ok.
  3. npm run build or yarn run build ... Now I will have my ./build directory created with index.html in it.
  4. Now I just open index.html in browser and it won't work.

I want to understand why this does not work? Why I have to serve the build to make it work?

like image 203
Gaurav51289 Avatar asked Sep 28 '17 00:09

Gaurav51289


People also ask

Can we change index HTML in React?

You only have to conditionally change the code of your public/index. html file based on the current environment, as follows: What happens is that the two sections written with the special syntax presented above will be included in the final HTML file only when process.

How to fix “homepage is not available” in React project?

To fix this problem go to your build folder then you will see manifest.json file, open it and you will see your manifest.json have: before your react project build go to your package.json file and specify homepage property with . value or maybe with your site base url, see example:

Why can't I see activity on my react app?

I server will be started from which you can access your react app Show activity on this post. If nothing of above works. Maybe the problem is that you are using react-router-dom and the routes needs a special compilation generating individual htmls for each page that exists in your router config.

What do you need to run a react app?

It's important to understand the things that need to run a React app. But due to it's Only one build dependency advantage, a beginner might think that react-scripts is the only dependency needed to run react apps and might not know that transpiler (babel), bundler (webpack) are the key dependencies which are used under the hood by react-scripts.

Where can I find the build folder in react-app?

http://myreact-app/build/index.html => White blank screen. This is the build folder which has been created after run npm run build. And this is the index.html


3 Answers

Following answer was given on similar topic by some guy on reddit

The project was built assuming it is hosted at the server root.

To override this, specify the homepage in your package.json.

I wouldn't recommended it but you can add the following to package.json to run it directly from your file server -

"homepage":"./"

https://www.reddit.com/r/reactjs/comments/5iya27/why_open_up_indexhtml_wont_workcreatereactapp/

also, You may want so serve the app on some kind of server more info here: https://create-react-app.dev/docs/deployment and here: https://create-react-app.dev/docs/production-build

like image 68
Marek Kamiński Avatar answered Sep 17 '22 14:09

Marek Kamiński


For react-router apps, there is a hack. Just make a <Route/> with path "/index.html" going to a suitable component.

<Route exact path="/" component={Home} /> <Route path="/index.html" component={Home} />

like image 29
Ankit Topno Avatar answered Sep 16 '22 14:09

Ankit Topno


For those using react-router-dom, you can use <HashRouter> instead of <BrowserRouter>. It should look like this:

<HashRouter>
  <Switch>
    <Route path="/" exact component={ Main } />
  </Switch>
</HashRouter>
like image 25
ChechoCZ Avatar answered Sep 18 '22 14:09

ChechoCZ