Create React App production build succeeds but produces code with errors

Viewed 1707

I have a create-react-app React application I've been developing using the webpack-dev-server, and everything works there in development.

However, when I build for production, the output build does not work and has the following console errors:

Uncaught SyntaxError: Unexpected token < :4001/static/js/1.33f1f515.chunk.js/:1
Uncaught SyntaxError: Unexpected token < :4001/static/js/main.625fef55.chunk.js/:1
Uncaught SyntaxError: Unexpected token < manifest.json:1 Manifest: Line: 1, column: 1, Unexpected token.
1 Answers

Are you using express (nodejs) server in production environment?

If yes then check static file serving configuration. Problem is /manifest.json, /static/js/main.625fef55.chunk.js/ etc are returning index.html instead of manifest or js file.

Here is my sample server file-

const express = require('express');
const path = require('path');
const app = express();

app.use('/', express.static(path.join(__dirname, '../build')));

app.use(function(req, res, next) {
  //console.log(req.originalUrl);
  next();
});

// to check if server is running
app.get('/ping', (req, res) => {
  res.json({
    success: true,
    message: 'pong'
  })
});

app.get('/*', function(req, res) {
  res.sendFile(path.join(__dirname, '../build', 'index.html'));
});

module.exports = app;
Related