Ionică Bizău Ionică Bizău - 3 months ago 29
Git Question

Install private dependencies via NPM

According to documentation, we can use git repositories as NPM dependencies:


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
.


That's fine and works like a charm, for public repositories. For private repositories (e.g. from GitHub), I tried this:

{
"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"
}


When I do
npm
install, I get the following output:

$ 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.


This error appears only for private repositories.

I have the access to this repository and doing
git clone ...
works, via SSH.

What's the solution for this issue?

Answer

As it says in the documentation, the git url needs to be in this form: git+ssh://user@hostname/project.git#commit-ish and I believe github needs to be accessed over ssh with the user git, thus your dependency url should probably be:

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