公司對于搭建本地私有npm庫有如下要求:
- 私有包托管在內部服務器中
- 項目中使用了公共倉庫上的公共包,也使用了內部服務器上的私有包
- 希望下載的時候,公共包走公共倉庫,私有包走內部服務器的私有倉庫
- 服務器硬盤有限,希望只緩存下載過的包,而不是全部同步
- 對于下載,發布npm包有對應的權限管理,安裝方便,配置簡單,依賴少
sinopia
sinopia是一個零配置帶緩存的npm包管理工具,符合上述要求
安裝過程及遇到的問題解決
安裝環境:
ubuntu 16.04LTS
node 7.8.0
npm 4.2.0
安裝配置node及npm
從官網下載https://nodejs.org/en/download/current/ 下載最新二進制壓縮包node-v7.8.0-linux-x64.tar.xz
放到當前用戶下面的目錄,并解壓
$ tar xvJf node-v7.8.0-linux-x64.tar.xz
進入bin目錄
$ cd bin
$ ./node -v
v7.8.0
說明nodejs已經安裝成功,為了方便執行命令,需要配置環境變量
$ sudo vi /etc/profile
在文件底部插入,保存
PATH=$PATH:/{prefix}/node-v7.8.0-linux-x64/bin
使之生效
$ source /etc/profile
這樣就可以在所有位置輸入命令,驗證node及npm安裝成功
$ node -v
v7.8.0
$ npm -v
v4.2.0
可以先配置下npm的源地址
$ npm set registry https://registry.npm.taobao.org/ # 推薦淘寶npm鏡像
安裝配置sinopia
$ npm install -g sinopia
安裝完成,啟動
$ sinopia
warn --- config file - /home/{user}/.config/sinopia/config.yaml
warn --- http address - http://localhost:4873/
上面信息第一行是生成的配置文件,第二行是默認地址,瀏覽器打開可看到
配置文件能完成如下等功能:
- 設置包存放路徑
- 設置用戶信息文件路徑及數量
- 設置上游地址
- 設置包發布、安裝權限
- 設置監聽地址
配置文件config.yaml如下:
#
# This is the default config file. It allows all users to do anything,
# so don't use it on production systems.
#
# Look here for more config file examples:
# https://github.com/rlidwka/sinopia/tree/master/conf
#
# path to a directory with all packages
storage: /home/{user}/.local/share/sinopia/storage # 庫存路徑,需要考慮磁盤空間
web: # 自定義web項,即瀏覽器訪問頁面
# web interface is disabled by default in 0.x, will be enabled soon in 1.x
# when all its issues will be fixed
#
# set this to `true` if you want to experiment with web ui now;
# this has a lot of issues, e.g. no auth yet, so use at your own risk
#enable: true
title: Sinopia
# logo: logo.png
# template: custom.hbs
auth:
htpasswd:
file: ./htpasswd # 添加用戶(npm adduser)后自動創建,保存用戶信息,可以初始化用戶
# Maximum amount of users allowed to register, defaults to "+inf".
# You can set this to -1 to disable registration.
#max_users: 1000 # 設置為-1不能npm adduser
# a list of other known repositories we can talk to
uplinks: # 可以配置多個上游地址,后面packages中的proxy指定用哪個
npmjs:
url: https://registry.npm.taobao.org/ # 更改此上游地址
# amount of time to wait for repository to respond
# before giving up and use the local cached copy
#timeout: 30s # 請求上游地址超時時間
# maximum time in which data is considered up to date
#
# default is 2 minutes, so server won't request the same data from
# uplink if a similar request was made less than 2 minutes ago
#maxage: 2m # 包過期時間
# if two subsequent requests fail, no further requests will be sent to
# this uplink for five minutes
#max_fails: 2 # 容許依賴請求最大失敗數
#fail_timeout: 5m # 依賴請求超時時間
packages: # 包的權限管理,$all為所有人,$authenticated為通過驗證人
# 分布和安裝兩種權限,值可以特指某幾人
'@*/*': # 跟package.json中的name屬性進行匹配
# scoped packages
access: $all
publish: $authenticated
'*':
# allow all users (including non-authenticated users) to read and
# publish all packages
#
# you can specify usernames/groupnames (depending on your auth plugin)
# and three keywords: "$all", "$anonymous", "$authenticated"
access: $all
# allow all known users to publish packages
# (anyone can register by default, remember?)
publish: $authenticated
# if package is not available locally, proxy requests to 'npmjs' registry
proxy: npmjs
# log settings
logs:
- {type: stdout, format: pretty, level: http}
#- {type: file, path: sinopia.log, level: info}
listen: 0.0.0.0:4873 # 設置監聽地址,0.0.0.0匹配本機地址
# if you use nginx with custom path, use this to override links
#url_prefix: https://dev.company.local/sinopia/
# Configure HTTPS, it is required if you use "https" protocol above.
#https:
# key: path/to/server.key
# cert: path/to/server.crt
# you can specify proxy used with all requests in wget-like manner here
# (or set up ENV variables with the same name)
#http_proxy: http://something.local/ # 設置代理服務器
#https_proxy: https://something.local/
#no_proxy: localhost,127.0.0.1
# maximum size of uploaded json document
# increase it if you have "request entity too large" errors
#max_body_size: 1mb # http請求body大小
修改uplinks及listen值,同上,重啟sinopia
如果想引用別的配置文件,請通過sinopia -c <配置文件>指定
最后更改npm源地址為私有庫地址
$ npm set registry http://{服務器ip}:4873/ # 內網測試可行(外網暫無條件測試)
用pm2托管sinopia
上面方式啟動sinopia只是暫時的,退出命令行就沒有了,因此需要一個長期開啟sinopia方案,通過pm2托管,可以讓sinopia進程永遠活著,就算意外掛了也可自動重啟。
安裝pm2
$ npm install -g pm2
安裝完后,使用pm2啟動sinopia
$ pm2 start sinopia
[PM2] Spawning PM2 daemon with pm2_home=/home/{user}/.pm2
[PM2] PM2 Successfully daemonized
...
[PM2] Done.
pm2提供開機自啟動功能
$ pm2 startup ubuntu # 或centos,看具體環境
將圖片紅圈內容復制運行
最后保存配置信息
$ pm2 save
到此為止,居于sinopia的npm私有庫搭建完成了,當下次啟動linux時,npm私有庫已經自動運行了
客戶端操作
nrm的使用
nrm是個方便的npm源管理工具,來做快速registry切換,可選
$ npm install -g nrm
$ nrm ls
npm ---- https://registry.npmjs.org/
cnpm --- http://r.cnpmjs.org/
taobao - https://registry.npm.taobao.org/
nj ----- https://registry.nodejitsu.com/
rednpm - http://registry.mirror.cqupt.edu.cn/
npmMirror https://skimdb.npmjs.com/registry/
edunpm - http://registry.enpmjs.org/
加入新創建的私有庫地址,并切換
$ nrm add xgnpm http://{ip}:{port}/
$ nrm use xgnpm
創建npm用戶
在發布包之前,一般需要創建用戶
$ npm adduser # 注冊,根據提示創建完成
$ npm login
創建用戶后,默認會在配置文件同級目錄生成htpasswd,保存用戶信息
發布
簡單創建一個npm包
$ mkdir publishtest
$ cd publishtest
$ npm init
$ vi index.js
$ ls
index.js package.json
$ npm publish
打開瀏覽器,發現這個包已經上傳至服務器
再看看這個包的實際路徑
若要取消發布,執行
$ npm unpublish
升級
要升級包,只需更改package.json的version屬性,version要比原來的大才行
安裝
安裝會先找本地有無該包,沒有的話從上游地址下載,然后保存到storage中,下次安裝直接從本地獲取
$ npm install publishtest01
會生成node_modules,里面就是剛才發布的包
補充:windows下安裝sinopia
windows環境中基本與linux下相同,但可能會遇到幾個問題
- 需要安裝python2.7
下載安裝python2.7,配置環境變量PATH,npm config set python python2.7 - MSBuild版本不對
安裝Visual Studio Express 2013 for Windows Desktop可以解決 - 安裝sinopia時出現錯誤
crypt3跟fs-ext模塊安裝錯誤可以忽略,windows不支持 - windows下pm2無效,用nssm來開啟服務
下載nssm,解壓,命令行運行nssm install sinopia,跳出對話框,輸入配置
- path: node
- startup directory: C:\Users\Administrator\AppData\Roaming\npm\node_modules\sinopia
- arguments: C:\Users\Administrator\AppData\Roaming\npm\node_modules\sinopia\lib\cli.js -c C:\Users\Administrator.config\sinopia\config.yaml
然后按install service按鈕,最后在命令行運行sc start sinopia,服務自動開啟