安裝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.

 

假如我們的想要作為包被安裝的私有倉庫叫 'my_production'

- 'my_production'的git倉庫:

npm init

然后輸入版本號等各種信息,push代碼。

 

- 使用 'my_production' 的項目:

  - package.json:

  "dependencies": {
    "@babel/polyfill": "^7.4.4",
    "my_production": "git+https://git.xxx.com/xxx/my_production.git",
    "axios": "^0.18.0",
    "element-ui": "^2.8.2",

它的寫法有以下幾種(摘自npm官網):

 

 

 

此時它還缺少權限無法被正確 npm install。

  - package.json:

  "dependencies": {
    "@babel/polyfill": "^7.4.4", "my_production": "git+https://username:password@git.xxx.com/xxx/my_production.git", "axios": "^0.18.0", "element-ui": "^2.8.2",

此時可以正常下載了,可是由於npm有緩存機制,所以下載一次后如果你更新了 'my_production' 的代碼,再次運行npm install 是無法拉取到最新代碼的。

 

 

解決辦法可以添加版本控制:

在 'my_production' 倉庫中打 tag :

在使用 'my_production' 的項目中添加 tag 中的版本信息:  - package.json:

  "dependencies": {
    "@babel/polyfill": "^7.4.4",
    "my_production": "git+https://username:password@git.xxx.com/xxx/my_production.git#v1.0.1",
    "axios": "^0.18.0",
    "element-ui": "^2.8.2",

 

還可以在[使用包的項目的 package.json 文件]中直接寫 ['my_production' 項目的 package.json 文件]里的版本,其它信息寫在[使用包的項目的 package.json 文件]中的 package-lock.json 文件中。具體寫法本小白沒仔細研究就不寫了,大家自行谷歌或者照其它項目的寫法復制粘貼 try-try-see。:)

更多的使用方法可以去看 npmjs.com 官網

 


免責聲明!

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



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