【问题标题】:Why won't git bash recognize gh-pages as a valid command after npm installing gh-pages? [GatsbyJS, GitHub Pages]为什么在 npm 安装 gh-pages 后 git bash 不能将 gh-pages 识别为有效命令? [GatsbyJS,GitHub 页面]
【发布时间】:2022-02-17 11:14:40
【问题描述】:

我的问题

我正在尝试将 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"
  }
}

我的尝试

the Gatsby docs 之后,我运行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 期待看到一些东西,但它说找不到命令。

我找到this post 并运行npm cache clean --force,删除node_modulespackage-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`

【问题讨论】:

    标签: node.js bash npm github-pages gatsby


    【解决方案1】:

    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 &amp;&amp; node ./scripts/deploy-github"

    保存文件,执行npm run,您将看到可以执行的脚本列表。要部署时执行npm run deploy:github

    【讨论】:

      【解决方案2】:

      如果您希望通过 cli 调用 gh-pages,这里有两个选项。

      选项 A:全局安装 gh-pages

      npm install gh-pages --global
      
      gh-pages --help
      

      默认情况下,npm install 将包放在本地 ./node_modules/ 目录中。通过使用--global 开关,您可以更改该行为,以便更方便地访问命令。

      global
      
        o Default: false
      
        o Type: Boolean
      
        Operates in "global" mode, so that packages are installed into the prefix folder instead  of  the  current  working
        directory. See npm help folders for more on the differences in behavior.
      
        o packages are installed into the {prefix}/lib/node_modules folder, instead of the current working directory.
      
        o bin files are linked to {prefix}/bin
      
        o man pages are linked to {prefix}/share/man
      

      选项 B:在本地调用 gh-pages

      npm install gh-pages --save-dev
      
      node node_modules/gh-pages/bin/gh-pages.js --help
      

      按照the Gatsby Docs 中的建议,您运行了同样的命令。然后直接调用本地 gh-pages.js 的完整路径。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-06-12
        • 2015-06-04
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        • 2022-11-26
        • 2015-12-22
        • 2022-01-26
        相关资源
        最近更新 更多