npm 安装私有 git 包

npm 安装私有 git 包

npm 对于前端开发来讲是一种必备的工具,对于开源项目来讲,彻底没有任何问题,安装包的依赖直接依赖于 Npm 便可。
对于公司内网的一些项目就不是太方便了,由于咱们一般会有这样的项目结构:前端

项目结构

对于 npm 公用包来讲是比较方便的,直接引用便可。而内网的代码应该怎么引入呢?大概有如下几种方式:git

  1. npm 公有包
  2. npm 私有包
  3. 搭建 npm 私有服务器
  4. git 仓库

公有包确定是知足不了需求的,由于全部人都能下载包,也就意味着代码是被泄漏了,首先被排除。npm 私有包是收费的,
而搭建 npm 服务器也是须要必定成本的,因此最好的方案天然是采用 npm 安装 git 仓库的方式了。github

下看 npm 对于安装 git 仓库的命令:npm

npm install <git remote url>

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

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

若是咱们直接安装 github 上,使用网址的方式能够表示为:bash

npm install git+https://isaacs@github.com/npm/npm.git

看下 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.ssh

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.ide

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

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

相关文章
相关标签/搜索