git 从 gitlab 私有仓库安装 npm 模块
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22988876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Install npm module from gitlab private repository
提问by FredyC
We are using GitLabfor 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:
我们将GitLab用于我们的私人项目。有一些来自 github 的分叉库,我们想将它们安装为 npm 模块。直接从 npm 安装该模块是可以的,例如:
npm install git://github.com/FredyC/grunt-stylus-sprite.git
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.
...也能正常工作,但对 GitLab 做同样的事情,只是改变域就会出现这个错误。
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
[email protected]:library/grunt-stylus-sprite.git
. Running this against npm install
it tries to install git
module from npm registry.
从 GitLab 的网络界面,我有这个 URL
[email protected]:library/grunt-stylus-sprite.git
。针对npm install
它运行它会尝试git
从 npm 注册表安装模块。
However using URL: [email protected]: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"
.
但是使用 URL:[email protected]:library/grunt-stylus-sprite.git
突然要求我输入密码。我的 SSH 密钥不包含密码,所以我认为它无法加载该密钥。也许我错过了一些配置?密钥位于我的主目录中的标准位置,名称为"id_rsa"
.
I am on Windows 7 x64.
我在 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 v3 开始,内置了对 GitLab 和其他来源(BitBucket、Gist)的支持,您可以从中安装软件包。不幸的是,它只适用于公共的,所以它与此并不完全相关,但有些人可能会发现它很有用。
npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]
npm install gitlab:<gitlabname>/<gitlabrepo>[#<commit-ish>]
Check out documentation: https://docs.npmjs.com/cli/install
回答by sidhuko
You have the following methods for connecting to a private gitlab repository
您有以下连接到私有 gitlab 存储库的方法
With SSH
使用 SSH
git+ssh://[email protected]:Username/Repository#{branch|tag}
git+ssh://[email protected]/Username/Repository#{branch|tag}
With HTTPS
使用 HTTPS
git+https://[email protected]/Username/Repository#{branch|tag}
With HTTPS and deploy token
使用 HTTPS 和部署令牌
git+https://<token-name>:<token>@gitlab.com/Username/Repository#{branch|tag}
回答by isaacs
Instead of git://
, use git+ssh://
and npm should do the right thing.
而不是git://
, usegit+ssh://
和 npm 应该做正确的事情。
回答by Xaqron
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.
正如@felix 在评论中提到的(感谢@felix) usingdeploy token
与阅读gitlab
. 这种方式是令牌被泄露,攻击者只能读取该存储库而无法进行更改。
- Log in to your
GitLab
account. - Go to the project you want to create Deploy Tokens for.
- Go to Settings > Repository.
- Click on
Expand
on Deploy Tokens section. - Choose a name and optionally an expiry date for the token.
- Choose the desired scopes. <= select
read_repository
- Click on Create deploy token.
- Save the deploy token somewhere safe. Once you leave or refresh the page, you won't be able to access it again.
- 登录到您的
GitLab
帐户。 - 转到要为其创建部署令牌的项目。
- 转到设置 > 存储库。
- 单击
Expand
部署令牌部分。 - 选择令牌的名称和可选的到期日期。
- 选择所需的范围。<= 选择
read_repository
- 单击创建部署令牌。
- 将部署令牌保存在安全的地方。一旦离开或刷新页面,您将无法再次访问它。
Old answer
旧答案
Goto User Settings > Access Tokens
and create a new access token
with read_registry
permission.
转到User Settings > Access Tokens
并在access token
获得read_registry
许可的情况下创建一个新的。
Copy generated token
, we need it for our package.json
file.
复制生成token
,我们的package.json
文件需要它。
Now in package.json
add the dependency
as below:
现在package.json
添加dependency
如下:
"my-module": "git+https://Xaqron:[email protected]/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}
.
替换Xaqron
为您的用户名和token
生成的令牌。您可以在 url 末尾指定branch
和。tag
#{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.
注意:由于访问令牌位于package.json
有权访问此项目的任何人都可以读取存储库,因此我假设您的项目本身是私有的。
回答by Sephy
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.
虽然这个问题是关于 Gitlab 的,但这个问题在 google 搜索中排名很好,所以这里有一些关于如何解决我在 Github 上遇到的类似问题的更多信息。
For me, only changing the url didnt make it work. Here are the steps I had to take to fix this :
对我来说,仅更改 url 并没有使其工作。以下是我必须采取的步骤来解决这个问题:
git+ssh://[email protected]: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
git+ssh://[email protected]:owner/repo.git#master
- 创建部署密钥并将其添加到存储库
- 编辑 git config(
~/.ssh/config
如果文件不存在则创建文件)以强制使用 DeployKey 而不是默认的 ssh 密钥
After that the npm install simply worked. All the other options and solutions resulted of the npm install breaking
之后 npm install 就可以正常工作了。所有其他选项和解决方案都导致 npm install 中断
回答by chrisf
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.
对于偶然发现此问题的其他任何人,我根本无法让它通过 HTTPS 工作 - 似乎它不支持到 repo 的直接链接(例如https://git.domain.com/user/somerepo.git
),也不支持.tar
,.tar.bz
或.zip
存档版本。
It only seems to work with the .tar.gz
archive.
它似乎只适用于.tar.gz
存档。
Full example (with tagged version):
完整示例(带有标记版本):
https://git.domain.com/user/somerepo/repository/archive.tar.gz?ref=v1.2.3
https://git.domain.com/user/somerepo/repository/archive.tar.gz?ref=v1.2.3
回答by chovy
None of the other answers worked for me for a private gitlab.com repo...
对于私人 gitlab.com 存储库,其他任何答案都不适用于我...
This works however:
然而,这有效:
npm i -S git+ssh://[email protected]:<org>/<project>.git
npm i -S git+ssh://[email protected]:<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.
它只是来自项目页面的“克隆”输入字段的 git ssh clone url,并git+ssh://
添加到它的前面。
回答by shentaoy
For me set the package.json as below works.
对我来说,将 package.json 设置为如下有效。
"dependencies": {
"<module-name>": "git+http://<username>:<token>@url.git",
}
The token is get from your "Profile Settings - Access Token".
令牌是从您的“配置文件设置 - 访问令牌”中获取的。
回答by Sam Gleske
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.
据我所知,你哪里出错了是git://
协议。GitLab 仅支持 HTTP(s) 和 SSH 进行克隆。因此,您必须使用其中一种方法而不是 git 协议。