我搭私庫的緣由很簡單,目前正在開發一個組件庫,提供給公司內部使用,我不想去註冊npm,也不想等待npm的審覈,只想要有個倉庫快速測試發佈本身的npm包。vue
目前最方便的方案就是verdaccio,搭建很是方便,通常就幾分鐘就搞定了,須要的工具:node
接下來詳細介紹搭建的步驟。git
安裝verdaccio以前,我默認你們都已經安裝了nodejs和npm環境,這個就再也不贅述了。若是是本地搭建的話,直接進行下面的操做就能夠了。若是是在遠程服務器搭建,經過ssh鏈接遠程服務器就行。github
# 全局安裝 npm install verdaccio - g
修改配置的目的就是讓咱們的私庫能夠經過公網的ip訪問,首先查看npm全局安裝包的所在位置:web
npm root -g /usr/local/Cellar/node/8.4.0/lib/node_modules
其中/usr/local/Cellar/node/8.4.0/lib/node_modules即是咱們npm包全局安裝的地址。按如下命名查找配置文件所在的位置
而後npm
vim default.yaml
配置狀況以下vim
# # 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/verdaccio/verdaccio/tree/master/conf # # path to a directory with all packages storage: ./storage # path to a directory with plugins to include plugins: ./plugins web: # WebUI is enabled as default, if you want disable it, just uncomment this line #enable: false title: Verdaccio 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: 1000 # a list of other known repositories we can talk to uplinks: npmjs: url: https://registry.npmjs.org/ packages: '@*/*': # scoped packages access: $all publish: $authenticated proxy: npmjs '**': # 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 # You can specify HTTP/1.1 server keep alive timeout in seconds for incomming connections. # A value of 0 makes the http server behave similarly to Node.js versions prior to 8.0.0, which did not have a keep-alive timeout. # WORKAROUND: Through given configuration you can workaround following issue https://github.com/verdaccio/verdaccio/issues/301. Set to 0 in case 60 is not enought. server: keepAliveTimeout: 60 # To use `npm audit` uncomment the following section middlewares: audit: enabled: true # log settings logs: - {type: stdout, format: pretty, level: http} #- {type: file, path: verdaccio.log, level: info} # listen listen: 0.0.0.0:4873
最後一行爲新增的配置, 用於支持外網ip訪問bash
listen: 0.0.0.0:4873
而後輸入:wq保存並退出vim模式,啓動verdaccio服務便可。我通常會經過pm2啓動,緣由很簡單,關閉doc窗口後,服務不會停掉,而且能很好的管理咱們啓動的服務。服務器
經常使用命令ssh
咱們來啓動服務,查看效果:
pm2 start verdaccio
至此,npm的私庫搭建就完成了,圖中是我最新發布的一個基於vue的組件庫,後續會對組件庫的編寫和發佈作介紹,有興趣的朋友關注如下。