尝试在Windows 10中将Reactjs应用程序部署到GitHub时gh-pages失败

问题描述:

我尝试按照react文档中列出的确切说明进行操作,该说明如何将可正常运行的应用程序部署到github页面.当我运行 npm run deploy 时.它一直失败,直到gh-pages -d build说'gh-pages'不是内部或外部命令.我确保已安装了最新版本的node和npm

I tried following the exact instructions listed in the react documentation on how to deploy a working app to github pages. When I ran npm run deploy. It kept failing at the gh-pages -d build saying that the 'gh-pages' is not an internal or external command. I made sure I had the latest versions of node and npm installed

我已经使用-g标记安装了gh-pages,以使其全局可用.尝试添加到系统路径变量,该变量导致我知道gh-pages加载到的节点模块文件夹.还是一无所有.

I had installed gh-pages using the -g tag to make it globally available. Tried adding to the system path variable leading to the node modules folder where i knew gh-pages was loaded. Still nothing.

最后,我尝试从git bash窗口而不是cmd终端运行它.起初这不是我想到的,因为所有其他npm命令都起作用.不知道为什么这可以解决问题,但是确实可以解决.只需发布此内容,这样其他人就可以免于痛苦

Finally i tried running it from the git bash window instead of the cmd terminal. This hadn't occurred to me at first as all of the other npm commands worked. Don't know why this fixed things but it did. Just posting this so somebody else might be spared the pain

我遇到了同样的问题,但最终设法使它起作用.主要问题是我尚未安装Windows版Git,但我采取了一些额外的步骤来确保一切正常.

I had the same issue but finally managed to make it work. The main issue was that I hadn't installed Git for Windows, but I took some extra steps to make sure everything works fine.

  1. 从此处下载并安装适用于Windows的Git
  2. 运行npm cache clean清理npm缓存
  3. 运行npm install npm@latest -g安装最新版本的npm
  4. "homepage" : "http://myname.github.io/myapp",添加到 "package.json"文件,并使用正确的GitHub页面URL替换URL.如果不确定URL,请在浏览器中打开项目存储库,然后转到设置"选项卡.向下滚动并在"GitHub页面"下找到正确的URL.
  5. 运行npm install --save-dev gh-pages以安装gh-pages模块 将以下两行添加到"package.json"文件中的脚本"对象中:

  1. Download and install Git for Windows from here
  2. Run npm cache clean to clean npm cache
  3. Run npm install npm@latest -g to install the latest version of npm
  4. Add "homepage" : "http://myname.github.io/myapp", to "package.json" file and replace the URL with correct GitHub pages URL. If you are not sure about the URL, open the project repository in the browser and go to "Settings" tab. Scroll down and find correct URL under "GitHub Pages".
  5. Run npm install --save-dev gh-pages to install gh-pages module Add the following 2 lines to "scripts" object in "package.json" file:

"predeploy":"npm run build",

"predeploy": "npm run build",

部署":"gh-pages -d构建"

"deploy": "gh-pages -d build"

最后,运行npm run deploy以创建优化的生产 在GitHub页面上构建和部署它.如果您尚未登录 Github for Windows应用之前,Github应用弹出,并要求您输入用户名和密码.完成后,部署过程将继续并将生产构建版本上传到GitHub页面. 希望对您有所帮助,编码愉快.

Finally, run npm run deploy to create an optimized production build and deploy it on GitHub pages. If you haven't logged in to Github for windows app before, Github app pops up and asks you to enter your username and password. When you've done that the deploy process will continue and uploads the production build on GitHub pages. Hope that helps, happy coding.