I am trying to host a React Web Application using Firebase’s Hosting service, but I am getting this error in console: Uncaught SyntaxError: Unexpected token ‘<‘. When locally hosted and serving the build files to locally host the Application works as expected and so does the default react app.
I resorted to trying to host Reacts basic starter page with a fresh project which results in the same issue and I’ve done the following steps below:
- I have tried specifying the homepage url in package.json, tried using “.” as homepage.
- I have used cache busting to ensure caching isn’t an issue
- If I try accessing the JS file directly I receive the same error:
Uncaught SyntaxError: Unexpected token ‘<‘ (at main.19a48311.js:1:1) and the js file is return as a text/html MIME type. - If I enforce the MIME types in firebase.json accessing the JS file returns the HTML code on the page. And returns 404s for JS and CSS files when accessing the home page.
This is index.html in the build directory:
<!doctype html><html lang="en"><head><meta charset="utf-8"/><link rel="icon" href="/favicon.ico"/><meta name="viewport" content="width=device-width,initial-scale=1"/><meta name="theme-color" content="#000000"/><meta name="description" content="Web site created using create-react-app"/><link rel="apple-touch-icon" href="/logo192.png"/><link rel="manifest" href="/manifest.json"/><title>React App</title><script defer="defer" src="/static/js/main.19a48311.js"></script><link href="/static/css/main.f855e6bc.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>
This is firebase.json:
{
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
],
"headers": [
{"source": "/service-worker.js", "headers": [{"key": "Cache-Control", "value": "no-cache"}]}
]
}
}
This is package.json:
{
"name": "react-app",
"version": "0.1.0",
"private": true,
"homepage": ".",
"dependencies": {
"@testing-library/jest-dom": "^5.17.0",
"@testing-library/react": "^13.4.0",
"@testing-library/user-event": "^13.5.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-scripts": "5.0.1",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}
I also attached a SS of the build directory.
Any suggestions are welcome.
Gary Chhina is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.