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

1、首先在码云中创建一个仓库,如下图所示:

2、输入仓库名称以及其他的信息,如下图所示:

3、此时会有一个网址,如下图所示:

4、然后在dos中使用如下代码来clone:

git clone https://gitee.com/sunxj888/widget.git

如下图所示:

 

5、此时已经clone本地,如下图所示:

6、添加 package.json配置, 注意限定 @scope:

npm init --scope=widget

然后输入各种信息,包名、版本号等等,如下图所示:

7、然后提交 push 上去,首先通过git add .将当前添加的文件添加到本地仓库中,如下图所示:

8、然后commit将本地更新到仓库,如下图所示:

9、然后使用git push到远程仓库中,输入用户名、密码,如下图所示:

10、此时在看码云中的仓库已经push上来了,如下图所示:

12、然后在本地创建一个npm工程,使用如下命令创建:

ng new myPrivate --skip-npm

如下图所示:

13、此时创建好之后工程目录如下图所示:

14、查看node_modules包,如下图所示:

15、然后在相应的package.json中添加私有的npm包,输入:git+仓库位置,如下图所示:

16、此时在执行npm install时出现错误,提示是没有权限,需要输入用户名和密码,如下图所示:

17、那么在package.json包中只需要在网址中加入密码即可,格式为:git+https://username:passworld@gitee.com/sunxj888/widget.git即可,如下图所示:

18、然后在次执行npm install即可,如下图所示:

19、此时在node_modules中就会有自己的私有nmp包,如下图所示:

20、最后看下 npm 安装 git 仓库的协议:

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

<protocol> is one of git, git+ssh, git+http, git+https, or git+file.

If #<commit-ish> is provided, it will be used to clone exactly that commit. If the commit-ish has the format #semver:<semver><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 #<commit-ish>or #semver:<semver>is specified, then master is used.

即 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 为标签进行安装,这样确保代码是正确的

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

参考了:

https://www.douban.com/note/617409014/

https://www.cnblogs.com/dreamless/p/8616670.html

### 使用 npmGitHub 拉取项目 当需要通过 npm 安装来自 GitHub 的特定仓库作为依赖项时,可以通过指定 Git URL 来实现这一目标。Git URL 可以采用 SSH、HTTPS 或 git 协议的形式。 #### 方法一:使用 HTTPS 方式安装 对于大多数用户而言,最简单的方法是利用 HTTPS 进行克隆,这样无需配置额外的 SSH 密钥: ```bash npm install git+https://github.com/{username}/{repo}.git#{version/tag/branch} ``` 例如,要安装 `nhn/raphael` 库的一个具体版本 v2.3.0,则命令如下所示[^3]: ```bash npm install git+https://github.com/nhn/raphael.git#v2.3.0 ``` #### 方法二:使用 SSH 方式安装 如果已经设置了与 GitHub 关联的 SSH 密钥,并希望通过更安全的方式访问私有库或者避免输入用户名密的情况,可以选择 SSH 形式的路径: ```bash npm install git+ssh://git@github.com:{username}/{repo}.git#{version/tag/branch} ``` 同样地,针对上述例子中的 raphael 库,对应的指令变为: ```bash npm install git+ssh://git@github.com/nhn/raphael.git#v2.3.0 ``` 需要注意的是,在某些情况下可能会遇到权限被拒绝的问题,这通常是因为本地计算机未能成功连接至远程服务器所致。此时应确认已正确设置 SSH Key 并能够正常登录 GitHub 账户[^2]。 另外,为了处理特殊场景下的认证需求(比如多个账户),还可以借助环境变量 `GIT_SSH_COMMAND` 设置自定义的身份验证文件位置: ```bash GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa_github' npm install git+ssh://git@github.com:{username}/{repo}.git#{version/tag/branch} ``` 完成以上任一步骤后,即可将所需的 GitHub 项目加入到当前 Node.js 工程当中;随后执行 `npm install` 命令刷新所有依赖关系表单,确保新引入的内容得以生效[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值