Firebase deployed react app not working. Uncaught SyntaxError: Unexpected token '<'
Firebase deployed react app not working. Uncaught SyntaxError: Unexpected token '<'
当我 运行 使用 npm start
我的开发服务器时,我在 http://localhost:3000/ 中完美地 运行 拥有我的应用程序。但是,当我 npm run build
和 firebase deploy
应用程序时,应用程序未 运行ning 并抛出错误:未捕获的语法错误:意外的令牌 '<'.
找到我的 firebase.json:
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
我试着添加这个,以防万一没有成功捕获:
"headers": [
{
"source" : "/sw.js",
"headers" : [
{
"key" : "Cache-Control",
"value" : "no-store"
}
]
}
]
还有我的package.json:
{
"name": "marioplan",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
"firebase": "^8.2.1",
"moment": "^2.29.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-redux-firebase": "^3.0.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"redux": "^4.0.5",
"redux-firestore": "^0.14.0",
"redux-thunk": "^2.3.0",
"web-vitals": "^0.2.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"
]
}
}
我阅读了一些关于“主页”的内容并进行了试验:需要在 packaje.jason 中指定,因为 npm run build
抛出:
该项目是在假设托管在 / 的情况下构建的。
您可以使用 package.json.
中的主页字段进行控制
我也尝试删除 npm --prefix \"$RESOURCE_DIR\" run lint
predeploy 命令,因为我不清楚它的作用也没有不同的结果。
为了防止有人疑惑,我会检查文件是否实际部署在 firebase 托管控制台中。
您没有提供任何 JavaScript,而是 HTML,这就是意外 <
的来源。我会责怪这个重写规则,它提供了“有限”的意义,因为它总是服务于 index.html
:
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
当我 运行 使用 npm start
我的开发服务器时,我在 http://localhost:3000/ 中完美地 运行 拥有我的应用程序。但是,当我 npm run build
和 firebase deploy
应用程序时,应用程序未 运行ning 并抛出错误:未捕获的语法错误:意外的令牌 '<'.
找到我的 firebase.json:
{
"functions": {
"predeploy": [
"npm --prefix \"$RESOURCE_DIR\" run lint"
]
},
"hosting": {
"public": "build",
"ignore": [
"firebase.json",
"**/.*",
"**/node_modules/**"
],
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]
}
}
我试着添加这个,以防万一没有成功捕获:
"headers": [
{
"source" : "/sw.js",
"headers" : [
{
"key" : "Cache-Control",
"value" : "no-store"
}
]
}
]
还有我的package.json:
{
"name": "marioplan",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.11.6",
"@testing-library/react": "^11.2.2",
"@testing-library/user-event": "^12.6.0",
"firebase": "^8.2.1",
"moment": "^2.29.1",
"react": "^17.0.1",
"react-dom": "^17.0.1",
"react-redux": "^7.2.2",
"react-redux-firebase": "^3.0.0",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.1",
"redux": "^4.0.5",
"redux-firestore": "^0.14.0",
"redux-thunk": "^2.3.0",
"web-vitals": "^0.2.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"
]
}
}
我阅读了一些关于“主页”的内容并进行了试验:需要在 packaje.jason 中指定,因为 npm run build
抛出:
该项目是在假设托管在 / 的情况下构建的。 您可以使用 package.json.
中的主页字段进行控制我也尝试删除 npm --prefix \"$RESOURCE_DIR\" run lint
predeploy 命令,因为我不清楚它的作用也没有不同的结果。
为了防止有人疑惑,我会检查文件是否实际部署在 firebase 托管控制台中。
您没有提供任何 JavaScript,而是 HTML,这就是意外 <
的来源。我会责怪这个重写规则,它提供了“有限”的意义,因为它总是服务于 index.html
:
"rewrites": [
{
"source": "**",
"destination": "/index.html"
}
]