Create-React-App build - "Uncaught SyntaxError: Unexpected token <"
just remove
"homepage": "your app url "
from package.json to fix it
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.
- You will map the solution folder with the main Web app i.e., somedomain.net
- You will convert the Web API project to Application from IIS.
- Then you will convert the build folder of React App to web App just like Web API
- To make front-end App working specify the
"homepage": "somedomain.net/React-Project/Client-App/build"
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.
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.