npm 安装私有 git 包

本文详细介绍了如何在npm中安装需要账号密码的私有Git包,包括在`package.json`中配置带凭证的URL,以及npm安装Git仓库的命令格式。同时,讨论了使用特定版本或分支的方法,以及可能遇到的问题,如安装时不会自动获取最新代码。建议在开发中使用明确的tag来确保代码稳定性,并提到了私有仓库使用公共账号密码可能带来的管理挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

npm 安装私有 git 包 即 package.json中引入需要账号和密码的远程git插件

1、案例

公司内部做了一个组件库,放在了gogs(gogs 是一款极易搭建的自助 Git 服务)上,但是在项目中怎么引入呢?
引入方式如下:

"devDependencies": {
   "@anyi/anyi-ui": "git+http://loader:loader@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

因为我们gogs是有账户名和密码的,所以在地址中加入了账户名和密码,上面链接中的loader:loader,代表的就是用户名和密码,前面是用户名,后面是密码
例如:用户名是xiaohua,密码:xiaohua123,那我们就可以这样来引入git插件

"devDependencies": {
   "@anyi/anyi-ui": "git+http://xiaohua:xiaohua123@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

后面执行npm install即可。

通过上面的案例,我们大致了解了在项目如何配置远程git插件的url。

2、下面我们看一下 npm 对于安装 git 仓库的命令

npm install <git remote url>

实际上就是直接 install 一个 URL 而已。对于一些公有仓库, npm 还是做了一些集成的,比如 github等(示例全部出自 npm 官方文档):

npm install github:mygithubuser/myproject
npm install bitbucket:mybitbucketuser/myproject
npm install gitlab:myusr/myproj#semver:^5.0

如果我们直接安装 github 上,使用网址的方式可以表示为:

npm install git+https://github.com/shiqingyun1024/vue-summary.git

看下 npm 安装 git 仓库的协议:

<protocol>://[<user>[:<password>]@]<hostname>[:<port>][:][/]<path>[#<commit-ish> | #semver:<semver>]

我们来一个一个分析:

1、<protocol> : 协议
<protocol> is one of git, git+ssh, git+http, git+https, or git+file.
例如:

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

对应案例中的git+http

"devDependencies": {
   "@anyi/anyi-ui": "git+http://xiaohua:xiaohua123@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

2、[<user>[:<password>]@]
前面是用户名,后面是密码,对应案例中的xiaohua:xiaohua123@

"devDependencies": {
   "@anyi/anyi-ui": "git+http://xiaohua:xiaohua123@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

3、<hostname>[:<port>][:][/] 主机名/域名 + 端口号
对应 案例中的 gogs.pms.anyi-tech.com/

"devDependencies": {
   "@anyi/anyi-ui": "git+http://xiaohua:xiaohua123@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

4、<path> 路径名
对应 案例中的 anyi-front/anyi-ui.git

"devDependencies": {
   "@anyi/anyi-ui": "git+http://xiaohua:xiaohua123@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

5、[#<commit-ish> | #semver:<semver>] semver 版本号
对应 案例中的 #1.2.37

"devDependencies": {
   "@anyi/anyi-ui": "git+http://xiaohua:xiaohua123@gogs.pms.anyi-tech.com/anyi-front/anyi-ui.git#1.2.37"
}

If # is provided, it will be used to clone exactly that commit. If the commit-ish has the format #semver:, can be any valid semver range or exact version, and npm will look for any tags or refs matching that range in the remote repository, much as it would for a registry dependency. If neither #or #semver:is specified, then master is used.

翻译如下
如果使用 #<commit-ish>,它将被用于克隆该提交。如果commit-ish的格式为#semver<semver>;, <semver>可以是任何有效的semver范围或确切版本,NPM将在远程存储库中查找与该范围匹配的任何标记或引用,就像它查找注册表依赖项一样。如果没有指定#<commit-ish>或#semver:<semver>,则使用master。

即 protocol 支持 git, git+ssh, git+http, git+https, git+file,私有仓库需要用户名和密码时需要填写用户名和密码,semver 表示需要使用的版本号, 不过貌似不生效。(npm 中有个包 semver 是专门用于比较包的版本号大小)

直接写 #branch 表示需要安装的分支号。

所以在开发过程中我们可以这么写包:

npm i git+https://username:password@git.example.com/path/reposity#master

或者使用打的 tag

npm i git+https://username:password@git.example.com/path/reposity#1.0.0

可能存在的问题是:
由于新版的 npm install 在安装时会使用 package-lock.json, 有时候同一分支不会从 github 上拉取最新的,
可能需要手动再安装一下(拿自己的仓库试了下,果然不会更新),所以安装时尽量以 tag 为标签进行安装,这样确保代码是正确的

此外,由于私有仓库都是需要密码的,这个时候需要提供一个公共账号和密码,某种程度上不利于管理吧

参考:从码云上通过git安装私有npm包
npm 安装私有 git 包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值