通过NPM安装私有依赖项

时间:2015-03-01 17:56:08

标签: node.js git github

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

  

Git网址可以是以下形式:

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
     

commit-ish可以是任何标记,sha或分支,可以作为git checkout的参数提供。默认值为master

对于公共存储库来说,这很好,就像一个魅力。对于私人存储库(例如来自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 ...工作。

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

3 个答案:

答案 0 :(得分:5)

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

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

答案 1 :(得分:1)

对我来说,只更改网址并没有让它发挥作用。以下是我必须采取的步骤来解决这个问题:

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

之后,npm安装工作正常。所有其他选项和解决方案都是由npm安装破坏产生的

答案 2 :(得分:0)

我一直在使用Node.js提示符和Babun提示符,这对我来说很好。我在新存储库中使用“npm install”收到此错误。当我使用GitHub的Git Shell时效果很好。

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

相关问题