npm 安裝私有 git 包


npm 安裝私有 git 包

npm 對於前端開發來說是一種必備的工具,對於開源項目來說,完全沒有任何問題,安裝包的依賴直接依賴於 Npm 即可。
對於公司內網的一些項目就不是太方便了,因為我們通常會有這樣的項目結構:

項目結構

對於 npm 公用包來說是比較方便的,直接引用即可。而內網的代碼應該怎么引入呢?大概有以下幾種方式:

  1. npm 公有包
  2. npm 私有包
  3. 搭建 npm 私有服務器
  4. git 倉庫

公有包肯定是滿足不了需求的,因為所有人都能下載包,也就意味着代碼是被泄漏了,首先被排除。npm 私有包是收費的,
而搭建 npm 服務器也是需要一定成本的,所以最好的方案自然是采用 npm 安裝 git 倉庫的方式了。

下看 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://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.

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 為標簽進行安裝,這樣確保代碼是正確的

此外,由於私有倉庫都是需要密碼的,這個時候需要提供一個公共賬號和密碼,某種程度上不利於管理吧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM