Firebase 部署的 React 应用程序无法正常工作。未捕获的语法错误:意外的标记 '<'

问题描述 投票:0回答:2

当我使用

npm start
运行我的开发服务器时,我的应用程序在 http://localhost:3000/ 中完美运行。但是,当我
npm run build
firebase deploy
应用程序时,该应用程序未运行并抛出错误:未捕获的语法错误:意外的令牌'<'.

找到我的 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 中指定的 '“homepage”:' 的试验,因为

npm run build
抛出:

该项目是假设其托管在 / 上构建的。 您可以使用 package.json 中的主页字段来控制它。

我还尝试删除

npm --prefix \"$RESOURCE_DIR\" run lint
predeploy 命令,因为我不清楚它的作用,也没有不同的结果。

以防万一有人可能想知道,我会检查这些文件是否实际部署在 firebase 托管控制台中。

firebase firebase-hosting
2个回答
2
投票

您不提供任何 JavaScript,而是 HTML,这就是意外的

<
的来源。我会责怪这个重写规则,它提供了“有限”的意义,因为它总是有用的
index.html

"rewrites": [
  {
    "source": "**",
    "destination": "/index.html"
  }
]

0
投票

解决方案(至少在我的例子中)是构建项目,然后将其部署到 firebase 托管

npm run build 

然后

firebase deploy

© www.soinside.com 2019 - 2024. All rights reserved.