node.js - 构建Reactjs空白页面并且不生成其他页面

标签 node.js reactjs

我是reactjs新手,刚刚创建了我的第一个reactjs应用程序,我想制作生产版本,并使用了以下命令:

npm run build

但是生成的只是一个空白的index.html文件,为什么?因为我有很多路由,如何访问其他页面?为什么索引是空白的?

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "axios": "^0.19.0",
    "express": "latest",
    "nodemon": "^2.0.1",
    "react": "^16.9.0",
    "react-dom": "^16.9.0",
    "react-router-dom": "^5.0.1",
    "react-scripts": "3.1.0",
    "semantic-ui-css": "^2.4.1",
    "semantic-ui-react": "^0.88.1",
    "socket.io": "^2.3.0"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "universal-cookie": "^4.0.2"
  }
}

最佳答案

您需要添加

"homepage": "http://mywebsite.com/relativepath",

在你的 package.json 文件中(如果它托管在服务器上的某个地方)。 或者要在本地运行它,您需要输入以下命令

npm install -g serve
serve -s build

或者你在使用react-router时可以设置

编辑

"homepage": "."

在本地工作

了解更多信息read here

关于node.js - 构建Reactjs空白页面并且不生成其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59928729/

相关文章:

node.js - 等待所有查询完成并同时异步填充

node.js - 从 Node.js 远程写入 Cassandra 服务器时遇到问题

reactjs - React-bootstrap 弹出窗口 : Not positioned where I expect it

ruby-on-rails - Node.js - 在 HTTP GET 请求查询中发送时将数组转换为对象

mysql - node-mysql:多语句查询,ER_PARSE_ERROR

javascript - 使用 useReducer 和 useContext,在分派(dispatch)操作时不会重新呈现组件。如何触发重新渲染?

reactjs - 将 Ionic Native/Cordova 插件与 Ionic (React) 和 Capacitor 一起使用的正确方法是什么?

javascript - 无法在React JS中使用Unity WebGL背景div输入输入

javascript - 如何从 React hooks 返回父组件的回调?比如返回上传的url列表

node.js - Mocha 调用所有 "it"回调模拟(测试中间件)