02-react随记-在yarn build打包后,打开网页显示为空白的解决方案

react在yarn build打包后,打开网页显示为空白的解决方案

创建react项目

create-react-app my-app
cd my-app
npm start

构建完这个初始的项目之后,发现项目是可以正常访问的!

接着就是发布上线

yarn build

控制台中显示

The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.
You may serve it with a static server:

  serve -s build

Find out more about deployment here:

  https://cra.link/deployment

------------------------------------
该项目假设它位于服务器根目录下。
您可以使用package.json中的主页字段来控制它。

打开build文件中index.html文件,在浏览器中看到一片空白,打开调试,发现报错了

image-20201102170905423

解决方法

可以通过修改package.json文件的homepage属性来修改,

我们打开项目根目录下的package.json,发现没有homepage这个属性,这个时候就需要我们手动加上去,如下:重点: "homepage": "./"

{
  "name": "my-app2",
  "version": "0.1.0",
  "homepage": "./",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^5.11.4",
    "@testing-library/react": "^11.1.0",
    "@testing-library/user-event": "^12.1.10",
    "react": "^17.0.1",
    "react-dom": "^17.0.1",
    "react-scripts": "4.0.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"
    ]
  }
}

你可能感兴趣的:(02-react随记-在yarn build打包后,打开网页显示为空白的解决方案)