React站点发布到GitHub Pages运行gh-pages命令报错如何解决
提问内容
我正尝试将自己的React站点发布到GitHub Pages,站点地址为:https://kumarsreenivas051.github.io/kumar-site/
我将代码直接提交到了GitHub的master分支。
我的package.json配置内容如下:
{ "name": "kumar-site", "version": "0.1.0", "private": true, "dependencies": { "@material-ui/core": "^4.11.4", "@material-ui/icons": "^4.11.2", "@testing-library/jest-dom": "^5.11.10", "@testing-library/react": "^11.2.6", "@testing-library/user-event": "^12.8.3", "gh-pages": "^2.2.0", "react": "^17.0.2", "react-bootstrap-validation": "^0.1.11", "react-dom": "^17.0.2", "react-scripts": "4.0.3", "web-vitals": "^1.1.1" }, "scripts": { "predeploy": "npm run build", "deploy": "gh-pages -d build", "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" ] }, "homepage": "https://kumarsreenivas051.github.io/kumar-site" }
当我尝试运行gh-pages -d build命令时出现报错,日志内容如下:
14 verbose lifecycle kumar-site@0.1.0~deploy: CWD: C:\Users\Kumaraswamy V S\Documents\GitHub\kumarsreenivas051.github.io\kumar-site 15 silly lifecycle kumar-site@0.1.0~deploy: Args: [ '/d /s /c', 'gh-pages -d build' ] 16 silly lifecycle kumar-site@0.1.0~deploy: Returned: code: 1 signal: null 17 info lifecycle kumar-site@0.1.0~deploy: Failed to exec deploy script 18 verbose stack Error: kumar-site@0.1.0 deploy: `gh-pages -d build` 18 verbose stack Exit status 1 18 verbose stack at EventEmitter.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\index.js:332:16) 18 verbose stack at EventEmitter.emit (events.js:315:20) 18 verbose stack at ChildProcess.<anonymous> (C:\Program Files\nodejs\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14) 18 verbose stack at ChildProcess.emit (events.js:315:20) 18 verbose stack at maybeClose (internal/child_process.js:1048:16) 18 verbose stack at Process.ChildProcess._handle.onexit (internal/child_process.js:288:5) 19 verbose pkgid kumar-site@0.1.0 20 verbose cwd C:\Users\Kumaraswamy V S\Documents\GitHub\kumarsreenivas051.github.io\kumar-site 21 verbose Windows_NT 10.0.19043 22 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Program Files\\nodejs\\node_modules\\npm\\bin\\npm-cli.js" "run" "deploy" 23 verbose node v14.16.0 24 verbose npm v6.14.11 25 error code ELIFECYCLE 26 error errno 1 27 error kumar-site@0.1.0 deploy: `gh-pages -d build` 27 error Exit status 1 28 error Failed at the kumar-site@0.1.0 deploy script. 28 error This is probably not a problem with npm. There is likely additional logging output above. 29 verbose exit [ 1, true ]
我使用的操作系统是64位Windows 10,请问我该如何操作才能成功发布站点?
解决方案
- 首先验证本地git账号配置和GitHub是否一致,依次执行以下命令检查:
输出内容如果和你的GitHub账号信息不匹配,重新设置即可:git config --global user.name git config --global user.emailgit config --global user.name "你的GitHub用户名" git config --global user.email "你的GitHub绑定邮箱" - 升级gh-pages依赖版本,你当前使用的2.2.0版本和Node 14存在兼容问题,执行以下命令升级到最新版:
npm install gh-pages@latest --save-dev - 修改package.json中的deploy脚本,显式指定部署分支和仓库地址,避免自动识别出错:
"deploy": "gh-pages -d build -b gh-pages -r https://github.com/kumarsreenivas051/kumar-site.git" - 右键点击你使用的终端(cmd/PowerShell/VS Code终端),选择以管理员身份运行,切换到项目根目录后执行部署命令:
npm run deploy - 如果以上步骤还是报错,可使用手动部署方式:
- 先执行
npm run build生成静态资源打包文件 - 进入生成的build文件夹,依次执行以下命令:
git init git add . git commit -m "站点部署" git push -f https://github.com/kumarsreenivas051/kumar-site.git master:gh-pages - 先执行
部署完成后,进入GitHub仓库的Settings页面,找到Pages设置项,确认源分支为gh-pages,目录为根目录,等待3-5分钟即可正常访问站点。
内容的提问来源于stack exchange,提问作者Kumar
相关产品推荐
相关产品推荐

