由於業務安全須要等種種緣由,不可以把插件都發布到公共的npm倉庫,因此須要搭建本身的私有npm倉庫,最近本身搭建了一個簡單的npm倉庫,踩了些坑,和你們分享一下,但願可以幫到有須要的童鞋node
本次講述用sinopia從0開始搭建npm倉庫,講得會比較細,以爲囉嗦的童鞋能夠只看本身須要的哈([原文連接[1])linux
由於個人linux服務器是centos的,因此我這邊講的是一種在centos上安裝node 環境git
yum install -y wget
能夠在nodejs的官網找到最新的下載地址(找到須要的,右鍵複製連接地址就能夠拿到最新的地址啦)github
wget https://nodejs.org/en/download/node-v10.15.0-linux-x64.tar.xz
xz -d node-v10.15.0-linux-x64.tar.xz tar -xf node-v10.15.0-linux-x64.tar.xz
重點是要找到你的nodejs的文件路徑(你將node文件解壓到哪裏就是哪裏。),找不到node路徑的童鞋請執行npm
whereis node
而後執行centos
ln -s node路徑 /usr/bin/node ln -s node路徑 /usr/bin/npm eg: //個人node解壓路徑爲/opt/node-v10.15.0-linux-x64/bin/node ln -s /opt/node-v10.15.0-linux-x64/bin/node /usr/bin/node ln -s /opt/node-v10.15.0-linux-x64/bin/node /usr/bin/npm
若是出現瀏覽器
ln: failed to create symbolic link ‘/usr/bin/node’: File exists 執行:rm /usr/bin/node
node -v npm -v
裝好node之後,咱們就能夠在服務器直接安裝sinopia了,一行命令全局安裝安全
npm install -g sinopia
安裝好了能夠啓動試一下服務器
sinopia
出現下面的結果說明成功跑起來了,這個時候能夠打開http://localhost:4873/訪問了this
Sinopia doesn't need superuser privileges. Don't run it under root. warn --- config file - /root/.config/sinopia/config.yaml warn --- http address - http://localhost:4873/
若是報sinopia: command not found,請添加關聯,找到你的sinopia路徑(這個路徑在以前node路徑的子目錄裏面)
ln -s /opt/node-v10.15.0-linux-x64/bin/sinopia /usr/bin
node服務很是脆弱,通常在實際中使用都會配合守護進程。這裏我用的是 pm2 作守護進程
npm install -g pm2 pm2 -v
若是出現 pm2: command not found,和sinopia的方法同樣
ln -s /opt/node-v10.15.0-linux-x64/bin/pm2 /usr/bin
pm2 start `which sinopia` 結果相似以下: ┌─────────┬────┬──────┬────────┬────────┬─────┬────────┬───────────┐ │ Name │ id │ mode │ status │ ↺ │ cpu │ memory │ ├─────────┼────┼──────┼────────┼────────┼─────┼────────┼───────────┤ │ sinopia │ 0 │ N/A │ fork │ online │ 0 │ 0% │ 16.7 MB │ └─────────┴────┴──────┴────────┴────────┴─────┴────────┴───────────┘
默認狀況下,sinopia 的配置是不適合直接使用的,因此咱們須要對它的配置文件按需酌情修改
咱們找到上面提到的配置文件目錄,打開配置文件進行編輯
# # 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: ./storage #npm包存放的路徑 auth: htpasswd: file: ./htpasswd #保存用戶的帳號密碼等信息 # Maximum amount of users allowed to register, defaults to "+inf". # You can set this to -1 to disable registration. max_users: -1 #默認爲1000,改成-1,禁止註冊 # a list of other known repositories we can talk to uplinks: npmjs: url: http://registry.npm.taobao.org/ #默認爲npm的官網,因爲國情,修改 url 讓sinopia使用 淘寶的npm鏡像地址 packages: #配置權限管理 '@*/*': # scoped packages access: $all #表示哪一類用戶能夠對匹配的項目進行安裝 【$all 表示全部人均可以執行對應的操做,$authenticated 表示只有經過驗證的人能夠執行對應操做,$anonymous 表示只有匿名者能夠進行對應操做(一般無用)】 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 #如其名,這裏的值是對應於 uplinks # log settings logs: - {type: stdout, format: pretty, level: http} #- {type: file, path: sinopia.log, level: info} # you can specify listen address (or simply a port) listen: 0.0.0.0:4873 #默認沒有,只能在本機訪問,添加後能夠經過外網訪問
==listen: 0.0.0.0:4873 這一條必定得加,而後記得把服務器的4873的端口開放==
在本身的電腦瀏覽器上輸入服務器ip地址+端口號,端口號默認爲4873
eg:192.186.1.343:4873
到這裏,咱們能夠成功的在本身的服務器上搭建咱們的私有npm啦~~