Install npm module from gitlab private repository

2020-01-25 12:45发布

问题:

We are using GitLab for our private project. There are some forked libraries from github, that we want to install as npm module. Installing that module directly from npm is ok and for example this:

npm install git://github.com/FredyC/grunt-stylus-sprite.git

...works correctly too, but doing the same for GitLab, just changing domain gets me this error.

npm WARN `git config --get remote.origin.url` returned wrong result (git://git.domain.com/library/grunt-stylus-sprite.git)
npm ERR! git clone git://git.domain.com/library/grunt-stylus-sprite.git Cloning into bare repository 'D:\users\Fredy\AppData\Roaming\npm-cache\_git-remotes\git-git-domain-com-library-grunt-stylus-sprite-git-6f33bc59'...
npm ERR! git clone git://git.domain.com/library/grunt-stylus-sprite.git fatal:unable to connect to git.domain.com:
npm ERR! git clone git://git.domain.com/library/grunt-stylus-sprite.git git.domain.com[0: 77.93.195.214]: errno=No error
npm ERR! Error: Command failed: Cloning into bare repository 'D:\users\Fredy\App
Data\Roaming\npm-cache\_git-remotes\git-git-domain-com-library-grunt-stylus-spr
ite-git-6f33bc59'...
npm ERR! fatal: unable to connect to git.domain.com:
npm ERR! git.domain.com[0: xx.xx.xx.xx]: errno=No error

From the web interface of GitLab, I have this URL git@git.domain.com:library/grunt-stylus-sprite.git. Running this against npm install it tries to install git module from npm registry.

However using URL: git+ssh@git.domain.com:library/grunt-stylus-sprite.git is suddenly asking me for the password. My SSH key doesn't include passphrase, so I assume it wasn't able to load that key. Maybe there is some configuration for that I have missed ? Key is located at standard location in my home directory with the name "id_rsa".

I am on Windows 7 x64.

UPDATE

Since NPM v3 there is built-in support for GitLab and other sources (BitBucket, Gist), from where you can install packages. Unfortunately it works only for public ones so it's not exactly related to this, but some might find it useful.

npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]

Check out documentation: https://docs.npmjs.com/cli/install

回答1:

You have the following methods for connecting to a private gitlab repository

With SSH

git+ssh://git@git.mydomain.com:Username/Repository#{branch|tag}
git+ssh://git@git.mydomain.com/Username/Repository#{branch|tag}

With HTTPS

git+https://git@git.mydomain.com/Username/Repository#{branch|tag}

With HTTPS and deploy token

git+https://<token-name>:<token>@gitlab.com/Username/Repository#{branch|tag}


回答2:

Instead of git://, use git+ssh:// and npm should do the right thing.



回答3:

Update

As @felix mentioned in comments (thanks @felix) using deploy token is much more relevant for reading a private registry on gitlab. This way is the token is compromised, attacker just can read that repository and cannot make changes.

Creating a Deploy Token

  1. Log in to your GitLab account.
  2. Go to the project you want to create Deploy Tokens for.
  3. Go to Settings > Repository.
  4. Click on Expand on Deploy Tokens section.
  5. Choose a name and optionally an expiry date for the token.
  6. Choose the desired scopes. <= select read_repository
  7. Click on Create deploy token.
  8. Save the deploy token somewhere safe. Once you leave or refresh the page, you won’t be able to access it again.

Old answer

Goto User Settings > Access Tokens and create a new access token with read_registry permission.

Copy generated token, we need it for our package.json file.

Now in package.json add the dependency as below:

"my-module": "git+https://Xaqron:token@gitlab.com/Xaqron/my-module"

Replace Xaqron with your username and token with the generated token. You can specify branch and tag at the end of url by #{branch|tag}.

Note: Since access token is located in package.json anyone who has access to this project can read the repository, so I assume your project is private itself.



回答4:

Although the question is about Gitlab, this question is quite well ranked in google search, so here is some more information about how to fix a similar issue I got with Github.

For me, only changing the url didnt make it work. Here are the steps I had to take to fix this :

  • git+ssh://git@github.com:owner/repo.git#master
  • Create a deploy key and add it to the repo
  • Edit git config (~/.ssh/config create the file if it doesnt exist) to force the use of the DeployKey instead of the default ssh key

After that the npm install simply worked. All the other options and solutions resulted of the npm install breaking



回答5:

Just for anyone else who stumbles across this, I couldn't get it working over HTTPS at all - seems it doesn't support the direct link to the repo (e.g. https://git.domain.com/user/somerepo.git), nor does it support the .tar, .tar.bz or .zip archive versions.

It only seems to work with the .tar.gz archive.

Full example (with tagged version):

https://git.domain.com/user/somerepo/repository/archive.tar.gz?ref=v1.2.3



回答6:

As far as I can tell where you're going wrong is the git:// protocol. GitLab only supports HTTP(s) and SSH for clones. So you must use one of those methods instead of the git protocol.



回答7:

None of the other answers worked for me for a private gitlab.com repo...

This works however:

npm i -S git+ssh://git@gitlab.com:<org>/<project>.git

Its just the git ssh clone url from the project page's "clone" input field with git+ssh:// added to the front of it.



回答8:

For me set the package.json as below works.

"dependencies": {
    "<module-name>": "git+http://<username>:<token>@url.git",
}

The token is get from your "Profile Settings - Access Token".



标签: git npm gitlab