78

I realize this question has been asked multiple times but nothing has worked for me...

I'm trying to create a static build of a create-react-app project but I'm getting the following errors:

Uncaught SyntaxError: Unexpected token < 1.ca81c833.chunk.js:1 Uncaught SyntaxError: Unexpected token < main.7ced8661.chunk.js:1 

Due to these files being minified, I'm not sure where to begin in debugging them.

Per other SO responses, here are some things I've tried:

//Original index.html file, which gets included in the built file: <script type="text/babel" src="https://cdnjs.cloudflare.com/ajax/libs/babel-core/5.8.24/browser.min.js"></script> //package.json "babel-preset-es2015": "^6.24.1", "babel-preset-react": "^6.24.1", //.babelrc file { "presets": ["react", "es2015", "stage-1"] } 

Not sure if this is relevant, but I have this on my express server, which I believe is standard:

if (process.env.NODE_ENV === "production") { app.use(express.static('client/built')); app.get("*", (req, res) => { res.sendFile(require('path') .resolve(__dirname, 'client', 'build', 'index.html')); }) } 

Assuming it's actually a JSX issue, the whole thing is very confusing - shouldn't create-react-app be handling JSX automatically?

UPDATE: I just posted this question but already have a relevant update. I'm able to serve the page statically through pm2 by running pm2 serve build so I think the issue may be with my server configuration.

4
  • Where did this error occur? can you show us the page? Commented Jan 24, 2019 at 6:03
  • Are you asking for an actual URL? Commented Jan 24, 2019 at 6:05
  • Did this error occur right after installing? Commented Jan 24, 2019 at 6:20
  • Possible duplicate of stackoverflow.com/questions/28100644/… Commented Jan 24, 2019 at 6:30

11 Answers 11

80

Thanks this helped me a lot. Just wanting to add to this with an example from a Create-React-App project that had the same solution: I received the same error after deploying to heroku.

Uncaught SyntaxError: Unexpected token < after serve -s build

For me the problem was in the packages.json file. The "homepage" parameter i gave was incorrect. Changing this to the correct heroku URL solved the issue.

"homepage": "https://myapp.herokuapp.com/" 

Hope this addition is helpful.

Sign up to request clarification or add additional context in comments.

3 Comments

you're an absolute legend
Yes, i agree, you're an absolute legend :)
Or just add PUBLIC_URL=https://myapp.herokuapp.com/ as .env Environment variable
39

I ended up finding an answer here: https://github.com/facebook/create-react-app/issues/1812

I trimmed down the full solution from above, but I changed:

app.use(express.static('client/build')); app.get("*", (req, res) => { res.sendFile(require('path') .resolve(__dirname, 'client', 'build', 'index.html')); }) 

to:

const root = require('path').join(__dirname, 'client', 'build') app.use(express.static(root)); app.get("*", (req, res) => { res.sendFile('index.html', { root }); }) 

It's definitely a bit strange to me that the first block didn't work. I assume it has something to do with the relative links in my React project since I do get an index.html file delivered to browser, despite getting the error. Maybe a completely static file would work with the first block, but I'd be interested to know if that's accurate.

2 Comments

Perfect, thanx! My problem was that I forgot to include app.use(express.static('client/build'));
The question does not make an assumption of which server we use though. What if I have a .net core server?
36

just remove

"homepage": "your app url "

from package.json to fix it

5 Comments

yes worked for me, though don't fully understand why!
so when I DO need to serve from a different subpath what should I do? That's not really an acceptable solution.
You can check my answer :)
I cant find homepage in my package.json, if i host the site in port 5000 it works but it doesnt work in port 4000
spend 4 hours to come to this answer
20

Remove the "homepage": "app-url" from package.json. Absence of homepage in package.json will assume that it will be hosted at the server root, or you will serve the build with serve -s build.

And Yes, specifying homepage will be helpful when you are going to deploy the App in a sub-directory of the server root.

To host your app on the IIS with the name somedomain.net and your solution already has a Web API project.

  1. You will map the solution folder with the main Web app i.e., somedomain.net
  2. You will convert the Web API project to Application from IIS.
  3. Then you will convert the build folder of React App to web App just like Web API
  4. To make front-end App working specify the "homepage": "somedomain.net/React-Project/Client-App/build"

3 Comments

Thank you so much, I spent so much time figuring this out.
I cant find homepage in my package.json, if i host the site in port 5000 it works but it doesnt work in port 4000
you're a hero, take my +1
6

I created a build version of react app using "npm run build". I have a server (node/express). I wanted to include the build in server side and deploy to heroku. What i did is copied build folder to server root folder and used code in server side startup file:

app.get('/*', function (req, res, next) { if (!req.path.includes('api')) res.sendFile(path.join(__dirname, 'build', 'index.html')); else next(); }); 

I was getting the same error. So i just set the path for static contents at starting:

var app = express(); //here below app.use(express.static(path.join(__dirname, 'build'))); 

And my static index.html was served fine and was able to find resources css and js.

Comments

3

I had faced the same issue when deploying my react build to production. After spending hours trying to figure out what went wrong on a previously working code, I figured out a mistake I made in deployment.

I hadn't copied the folder static inside build to the server because I used scp build/* to copy the build folder in place of scp -r build/*.

I understand that this is not the exact answer to the question asked here. But I had tried out almost all possible options from answers given by experts here before I noticed the error I was making. Hence, adding this here as a pointer to anyone facing similar issue to verify the deployment steps as well.

1 Comment

You just saved "my life". Thank you so much
3

UPDATE:

Recently I need to deploy create-react-app project to subpath of client's domain which is http://example.com/foo/bar

This approach is using Nginx, React-Router.

  1. Add PUBLIC_URL to .env file.
 + PUBLIC_URL=/foo/bar 
  1. Add basename to <BrowserRouter>.
 - <BrowserRouter> + <BrowserRouter basename={process.env.PUBLIC_URL}> 
  1. Change your Nginx config.
 location /foo/bar { alias /path/to/build; try_files $uri /$uri /foo/bar/index.html; } 

Here is a create-react-app document about how to build with .env: Customizing Environment

Hope this solution helps!


Ran into the same issue when I want to deploy the static build of a create-react-app project to my linux server.

I solved with this issue comment on cra's github and the cra's official document about how to deploy production build.

For example:

I want to put the production build website under something like http://example.com/foo/bar.

When I deploy without changing any default settings, I will get this "Uncaught SyntaxError: Unexpected token <" error and nothing shows up on the screen.

Here is the solution:

  1. Add homepage parameter to your package.json.
 + "homepage": "/foo/bar" 
  1. Add "/foo/bar" to all of your static resources in css which will be like:
 .dummyimage { - content: url('/dummyimage.jpg'); + content: url('/foo/bar/dummyimage.jpg'); } 
  1. Add "/foo/bar" to all of your links and routes.
 - linkTo: '/contact' + linkTo: '/foo/bar/contact' 
  1. Changing a little of your serve program's code, in node.js/express it will be like:
 - app.use(express.static(path.join(__dirname, '/build'))); + app.use('/foo/bar/', express.static(path.join(__dirname, '/build'))); 
  1. Build again.

  2. Deploy build/ to the server.

  3. Restart your serve program, like node.js/express.

Problem solved!!

Hope this solution is helpful.

4 Comments

so any time I want to deploy to different url I have to change source code? This is not acceptable solution
Thanks for replying. I think different url means different project, and each project need their own settings.
Many times yes. But there are other times where you want to deploy your project on different customers' set ups.
Understood. My answer works for my situation and environment for now. I will update my answer till I found a way easier to change those settings. I guess global variables will be a good start. Anyway, thanks for your feedback!
3

Just remove homepage key in package.json and also don't forgot to remove the basename in BrowserRouter, if you're using the react router.

That's it. It's working

Comments

1

i have faced kind of same issue when i want deploy my react app to github-pages :- its need's follow few guidelines

  1. Repository name should be in small latter

  2. If project name same as repo name that usefull

  3. addd { "predeploy": "npm run build", "deploy": "gh-pages -d build" } on package.json

  4. add homepage script at the starting of the package.json

    { "homepage": "http://[Username].github.io/[reponame]", "name": "--", "version": "--", "private": boolean, }

Comments

1

As most of us have already suggested removing homepage property from package.json

Let me explain, why it worked in my case:

Earlier I had setup my project to be hosted on Github pages and as a result, it had homepage property set to something like "https://shubhamshd.github.io/supplyChainApp"

However as there are known navigational errors mainly related to BrowserRouter package on Github-pages, I had to switch to other hosting platforms.

And as I forgot to remove the homepage property, deployment did not work on any of the platforms like Vercel or Netlify.

It was after long hours of search and trial, that I finally stumbled upon this thread, specifically the @Shashwat Gupta and finally managed to resolve it by removing the unwanted homepage property.

Comments

0

If you are deploying your client to S3, when deploying with react-deploy-s3, assign the distribution-id from CloudFront

react-deploy-s3 deploy \ --access-key-id XXXXXXXXXXXXXXXXX \ --secret-access-key XXXXXXXXXXXXXXXXXXXXXXXX \ --bucket XXXXXXX \ --region us-east-1 \ --distribution-id XXXXXXXXXXXXX <--- 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.