Search code examples
javascriptreactjsnpmcreate-react-appnetlify

Issue in deployment of react app in netlify


  • I am trying to deploy react application in netlify as referred from the medium link: https://link.medium.com/nw9ZCh0lrV
  • so I used express server to define build scripts and setting application in production mode
  • Build scripts are creating in local machine and uploaded in netlify site
  • It shows message as successful deployment
  • on clicking on URL link, it shows status to GET request for build scripts as 404
  • netlify URL link: https://nostalgic-euclid-4f95ab.netlify.com
  • can you guys help me where I have done mistake with your suggestion
  • attached application screenshots below -providing code snippets below:

server.js

const express = require('express');
const favicon = require('express-favicon');
const app = express();

app.use(favicon(__dirname + '/build/favicon.ico'));
app.use(express.static(__dirname));
app.use(express.static(path.join(__dirname,'build')));
app.get('/*', function (req, res) {
    res.sendFile(path.join(__dirname, 'build', 'index.html'));
});

packagae.json

"scripts" : {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  }

enter image description here
enter image description here


Solution

  • A common mistake in a create react app is to add a homepage in the package.json file that does not match your site location. Once you do a production build, the assets are prepended with the value of any path after the domain.

    https://nostalgic-euclid-4f95ab.netlify.com/codehangar/react-interview/static/js/main.7ab6795d.chunk.js

    From the look of the path of the broken (404) asset, your homepage value looks something like:

      "homepage": "https://example.com/codehangar/react-interview",
    

    If you are going to include a homepage value, make sure it is the same as your site URL.

    In your case the value should be:

      "homepage": " https://nostalgic-euclid-4f95ab.netlify.com",
    

    Note:* Typically, leave this setting out of the file until you are live and have your domain set.

    Also:

    • No need to store the build folder in your repository (.gitignore). The build command will replace it on each deploy.
    • You will not need the server.js file you supplied.