通过 NPM 安装私有依赖

Install private dependencies via NPM

根据 documentation,我们可以使用 git 个存储库作为 NPM 依赖项:

Git urls can be of the form:

git://github.com/user/project.git#commit-ish
git+ssh://user@hostname:project.git#commit-ish
git+ssh://user@hostname/project.git#commit-ish
git+http://user@hostname/project/blah.git#commit-ish
git+https://user@hostname/project/blah.git#commit-ish

The commit-ish can be any tag, sha, or branch which can be supplied as an argument to git checkout. The default is master.

对于 public 个存储库来说,这很好,而且很有魅力。对于私有存储库(例如来自 GitHub),我试过这个:

{
  "name": "test",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "dependencies": {
      "builder": "git+ssh://github.com/IonicaBizau/myprivatemodule.git"
  },
  "author": "",
  "license": "ISC"
}

当我执行 npm 安装时,我得到以下输出:

$ npm i
npm WARN package.json test@1.0.0 No description
npm WARN package.json test@1.0.0 No repository field.
npm WARN package.json test@1.0.0 No README data
npm ERR! git clone ssh://github.com/IonicaBizau/myprivatemodule.git Cloning into bare repository '/home/....'...
npm ERR! git clone ssh://github.com/IonicaBizau/myprivatemodule.git Permission denied (publickey).
npm ERR! git clone ssh://github.com/IonicaBizau/myprivatemodule.git fatal: Could not read from remote repository.
npm ERR! git clone ssh://github.com/IonicaBizau/myprivatemodule.git 
npm ERR! git clone ssh://github.com/IonicaBizau/myprivatemodule.git Please make sure you have the correct access rights
npm ERR! git clone ssh://github.com/IonicaBizau/myprivatemodule.git and the repository exists.
npm ERR! Error: Command failed: Cloning into bare repository '/home/...'...
npm ERR! Permission denied (publickey).
npm ERR! fatal: Could not read from remote repository.
npm ERR! 
npm ERR! Please make sure you have the correct access rights
npm ERR! and the repository exists.

此错误仅针对私有存储库出现。

我可以访问此存储库并通过 SSH 执行 git clone ... 工作。

这个问题的解决方案是什么?

如文档中所述,git url 需要采用以下形式: git+ssh://user@hostname/project.git#commit-ish 我相信 github 需要通过 ssh 与用户 git 一起访问,因此您的依赖项 url 可能应该是:

"git+ssh://git@github.com/IonicaBizau/myprivatemodule.git"

对我来说,仅更改 url 并没有使它起作用。以下是我必须采取的解决此问题的步骤:

  • git+ssh://git@github.com:owner/repo.git#master
  • 创建部署密钥并将其添加到存储库
  • 编辑 git 配置(~/.ssh/config 如果文件不存在则创建该文件)以强制使用 DeployKey 而不是默认的 ssh 密钥

之后 npm install 就可以正常工作了。 npm 安装中断导致的所有其他选项和解决方案

我一直在使用 Node.js 提示符和 Babun 提示符,它们对我来说效果很好。我在新存储库上遇到了 "npm install" 的错误。当我使用来自 GitHub 的 Git Shell 时,它工作正常。

我已经设置了我的 SSH 密钥并且我安装的任何其他 npm 都没有这个问题。