52

I'm trying to build my react app using react's build tool. When I try to "npm start", the app works fine.

npm start

On http://localhost:3000 => I can access the application.

enter image description here

But when I build the application and try to access "index.html" file on the build folder, it doesn't work and I encounter with a white blank screen.

npm run build

http://myreact-app/build/index.html => White blank screen.

This is the build folder which has been created after run npm run build.

enter image description here

And this is the index.html

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
    <meta name="theme-color" content="#000000">
    <link rel="manifest" href="/manifest.json">
    <link rel="shortcut icon" href="/favicon.ico">
    <title>React App</title>
    <link href="/static/css/main.9a0fe4f1.css" rel="stylesheet">
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript> 
    <div id="root"></div>
    <script type="text/javascript" src="/static/js/main.46d8cd76.js"></script>
  </body>
</html>

Am I doing something wrong? Can't I access the built index.html file on my apache web server?

Josh
  • 2,232
  • 3
  • 15
  • 33
amone
  • 3,712
  • 10
  • 36
  • 53
  • @Sulthan I've added. I am also trying to figure out it but it is the output of "npm run build". No error in the console – amone Jun 05 '17 at 14:20
  • Does `main.js` contain only your code or does it also includes all dependencies? That should be trivial to check. – Sulthan Jun 05 '17 at 14:21
  • In case this helps anyone... my problem was that my server wasn't allowing access to the static files requested by `index.html` – Lasf Apr 09 '20 at 04:57

10 Answers10

45

Probably you've not noticed yet but I don't think your html file is able to import css and script files correctly. When I look at your file structure, I see the everything about build is under the build folder. But in your html file, there are slashes ("/") before the file paths. That's why browser is looking for those files under the parent of the "build". Try to remove slashes.

<!DOCTYPE html>
<html lang="en">
    <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no">
      <meta name="theme-color" content="#000000">
      <link rel="manifest" href="/manifest.json"><link rel="shortcut icon" href="/favicon.ico">
      <title>React App</title>
      <style></style>
      <link href="static/css/main.65027555.css" rel="stylesheet">
    </head>
    <body
      <noscript>You need to enable JavaScript to run this app.</noscript>
      <div id="root"></div>
      <script type="text/javascript" src="static/js/main.316f1156.js"></script>
    </body>
</html>
Josh
  • 2,232
  • 3
  • 15
  • 33
aravvn
  • 774
  • 8
  • 8
44

The above problem can be overcome if you add

"homepage":".",

in your package.json. This is not official but a good hack to follow.

https://github.com/facebookincubator/create-react-app/issues/1487

Rifky Niyas
  • 1,737
  • 10
  • 25
Manas Gond
  • 640
  • 7
  • 13
  • 2
    When linking some reference, you can post something useful like the most important part of the post, so your answer can be more valuable... – DaFois Dec 12 '17 at 13:12
  • 1
    Below is the source for above comment. May be it will help someone else. https://github.com/facebook/create-react-app/blob/master/packages/react-scripts/template/README.md#serving-the-same-build-from-different-paths – Kodin Jun 16 '18 at 19:44
  • 3
    Adding the updated link here : https://facebook.github.io/create-react-app/docs/deployment#serving-the-same-build-from-different-paths – Naveen Kumar G C May 20 '19 at 13:13
25

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:

"start_url": ".",

change it to

"start_url": "/",

there is alternative way to fix the problem:

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:

{
   ....
   "homepage": ".",
   ......
}
TechnicalKeera
  • 923
  • 10
  • 20
  • 5
    Adding `"homepage": "."` is what worked for me. I can see in the index.html file built it prefixed all the links with a '.' which was exactly the problem I was having, thank you! – James Webb Feb 21 '20 at 15:36
  • Can confirm. Changing start_url is not working. Changing homepage in package.json works for me. – Jim LK Yu May 18 '23 at 08:29
7

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.

Ref: I'm using create-react-app and I need to open build/index.html in browser directly without a server

In summary you need to use <HashRouter> instead <Router> and a <Switch> wrapper to your routes. For example:

<Switch>
  <Route exact path='/' component={WelcomePage} />
  <Route exact path='/game' component={GamePage} />
</Switch>

Consider that your routes will change to:

http://localhost:3000/#/ -> Root page
http://localhost:3000/#/game -> Other page
jose920405
  • 7,982
  • 6
  • 45
  • 71
5

you should try use the serve package here to run single page app.

  • npm install -g serve to install globally
  • serve help to see help texts
  • serve --single build to serve single page app. I server will be started from which you can access your react app
Isaac Sekamatte
  • 5,500
  • 1
  • 34
  • 40
0

You need to install local server plugin/extenstion.

If you are vscode user then search live server extension and install it. Then there will be "Go live" option in bottom of your editor.

For other editor user (atom/sublime) search for live server plugins.

Bimal Grg
  • 7,624
  • 2
  • 24
  • 21
0

Sometimes the reason that the content is not served is because the command of "serve -s" was executed from outside of the build directory. The correct way is to go into the build directory then execute "serve -s" from therein.

R G
  • 31
  • 4
0

I had an index.php in the webroot which prevented my app from running index.html.

Steve
  • 2,066
  • 13
  • 60
  • 115
0

I use HashRouter instead BrowserRouter & also need to add /# infront of every url like this:

href={`/#/blogs/${slug}`}

this works for me , thanks

Umer Aziz
  • 111
  • 5
0

Try using <HashRouter> instead of <Router>, and don't forget to import {HashRouter}.

Suraj Rao
  • 29,388
  • 11
  • 94
  • 103