node.js - 为什么在 npm 安装 gh-pages 后 git bash 不能将 gh-pages 识别为有效命令?[GatsbyJS,GitHub 页面]
问题描述
我的问题
我正在尝试将 GatsbyJS 静态站点部署到我的 GitHub 页面索引页面,但由于某种原因,终端/命令行无法识别gh-pages
为有效命令。我在 vscode 中使用 git bash 但也尝试过 Windows cmd。我期望当我运行我添加到的部署脚本时package.json
,该站点将发布到我的 GitHub 页面站点 .github.io。我收到各种错误消息,但没有找到类似问题的足够答案。
我的设置
package.json
:
{
"name": "gatsby-starter-dimension-v2",
"description": "Gatsby Starter - Dimension V2",
"version": "1.0.0",
"author": "Hunter Chang",
"dependencies": {
"gatsby": "^2.0.76",
"gatsby-plugin-manifest": "^2.0.24",
"gatsby-plugin-offline": "^2.0.25",
"gatsby-plugin-react-helmet": "^3.0.2",
"gatsby-plugin-sass": "^2.0.7",
"node-sass": "^4.11.0",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-helmet": "^5.2.0"
},
"keywords": [
"gatsby"
],
"license": "MIT",
"scripts": {
"build": "gatsby build",
"develop": "gatsby develop",
"format": "prettier --write '**/*.js'",
"test": "echo \"Error: no test specified\" && exit 1",
"deploy": "gatsby build && gh-pages -d public -b master"
},
"devDependencies": {
"gh-pages": "^2.1.1",
"prettier": "^1.14.2"
},
"repository": {
"type": "git",
"url": "https://github.com/gatsbyjs/gatsby-starter-default"
}
}
我试过的
在Gatsby 文档之后,我运行了npm install gh-pages --save-dev
,并将自定义部署脚本添加到我的package.json
文件中:
`{
"scripts": {
"deploy": "gatsby build && gh-pages -d public -b master"
}
}`
添加该脚本后,我运行npm run deploy
. 结果发布在这篇文章的末尾。
运行没有问题gatsby build
,所以我尝试运行gh-pages
期待看到一些东西,但它说找不到命令。
我找到了这篇文章并运行npm cache clean --force
,删除node_modules
和package-lock.json
,然后npm install
再次运行。
我一直在寻找类似的问题一段时间。有什么想法吗?这是我在这里的第一篇文章,请多多关照……
终端输出(每个命令)
$ gh-pages
bash: gh-pages: command not found
$ gh-pages -d public -b master
bash: gh-pages: command not found
$ npm run deploy
:
`> gatsby-starter-dimension-v2@1.0.0 deploy C:\Users\Benjamin\Desktop\repos\personal-website-gatsby
> gatsby build && gh-pages -d public -b master
<various success messages, minor warnings, and info's>
...
...
fatal: HttpRequestException encountered.
An error occurred while sending the request.
bash: /dev/tty: No such device or address
error: failed to execute prompt script (exit code 1)
fatal: could not read Username for 'https://github.com': No error
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! gatsby-starter-dimension-v2@1.0.0 deploy: `gatsby build && gh-pages -d public -b master`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the gatsby-starter-dimension-v2@1.0.0 deploy script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\Benjamin\AppData\Roaming\npm-cache\_logs\2019-09-16T00_04_41_443Z-debug.log`
`Benjamin@DESKTOP-5T102UF MINGW64 ~/Desktop/repos/personal-website-gatsby (master)
$ npm install gh-pages --save-dev
npm notice save gh-pages is being moved from dependencies to devDependencies
npm WARN eslint-config-react-app@4.0.1 requires a peer of eslint-plugin-flowtype@2.x but none is installed. You must install peer dependencies yourself.
npm WARN ts-pnp@1.1.4 requires a peer of typescript@* but none is installed. You must install peer dependencies yourself.
npm WARN tsutils@3.17.1 requires a peer of typescript@>=2.8.0 || >= 3.2.0-dev || >= 3.3.0-dev || >= 3.4.0-dev || >= 3.5.0-dev || >= 3.6.0-dev || >= 3.6.0-beta || >= 3.7.0-dev || >= 3.7.0-beta but none is installed. You must install peer dependencies yourself.
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@2.0.7 (node_modules\chokidar\node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@2.0.7: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
npm WARN optional SKIPPING OPTIONAL DEPENDENCY: fsevents@1.2.9 (node_modules\fsevents):
npm WARN notsup SKIPPING OPTIONAL DEPENDENCY: Unsupported platform for fsevents@1.2.9: wanted {"os":"darwin","arch":"any"} (current: {"os":"win32","arch":"x64"})
+ gh-pages@2.1.1
updated 1 package and audited 20651 packages in 58.504s
found 0 vulnerabilities`
解决方案
见https://www.npmjs.com/package/gh-pages#basic-usage
您需要做的是创建一个 js 脚本来使用该模块。我建议您在scripts
项目根目录下的目录下创建它。
粘贴您需要运行的代码。我推荐以下配置上传到公共目录:
ghpages.publish(
'public',
{
branch: 'master',
repo: '',
},
() => {
console.log('Deploy Complete!')
}
)
输入您的回购属性。=> 定义输出到控制台的回调函数。
接下来,打开项目目录根目录下的 package.json。您将看到使用默认 Gatsby 脚本定义的“脚本”,您需要添加一个用于部署。
在末尾添加以下内容:
deploy:github": "npm run build && node ./scripts/deploy-github"
保存文件,执行npm run
,您将看到可以执行的脚本列表。npm run deploy:github
要部署时执行。
推荐阅读
- css - 样式化数据表“搜索”
- python - 调用另一个命令并从提示符获取用户输入
- typescript - 无法记录继承类中定义的属性
- python - 使用 numpy 优化 Python 循环的最佳方法
- java - 可以在 AWS Lambda 上设置 JVM 内存设置吗?
- javascript - HTML文件无法识别外部链接js文件中定义的函数?
- r - 具有 3 个向量的成对 t 检验
- ruby-on-rails - taglib-ruby 如何通过主动存储添加和编辑标签?
- google-apps-script - 如何使用 Google Apps 脚本设置或更新联系人的照片?
- javascript - 如何使用自动增量键更新 IndexedDB 项目?