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 为标签进行安装,这样确保代码是正确的
此外,由于私有仓库都是需要密码的,这个时候需要提供一个公共账号和密码,某种程度上不利于管理吧
参考了: