前端人員如何在linux服務器上搭建npm私有庫

爲何要搭建npm私有庫?
  1. 爲了方便下載時,公共包走npmjs,私有包走內部服務器。
  2. npm包下載的速度較慢,搭建npm私有庫以後,會先操做私有庫中是否有緩存,有緩存直接走緩存,而不用從新再去請求一遍網絡。
哪一種方式適合你呢?

npm私有庫的搭建有不少種,具體哪一種方式適合,我選擇的方案是比較簡單的「使用verdaccio搭建npm私有庫」。node

先試着在本地搭建一個吧

  1. 準備工做

    咱們須要使用npm命令去安裝verdaccio,因此咱們必需要有node環境,node環境又依賴於python。所以,在搭建npm私有庫的準備工做就是去搭建node環境。python

    1. 檢測是否有node環境linux

      chenwentaodeiMac:ceair_wallet chenwentao$ node -v
      bash: node: command not found
      複製代碼
    2. 下載nodegit

    3. 安裝nodegithub

    4. 檢驗是否安裝成功web

      chenwentaodeiMac:ceair_wallet chenwentao$ node -v
      v10.15.1
      複製代碼
  2. 安裝啓動verdaccio
    1. 安裝verdaccionpm

      安裝速度緩慢的話,可使用淘寶鏡像,install時遇到permission denied,記得前面加sudo瀏覽器

      chenwentaodeiMac:ceair_wallet chenwentao$ sudo cnpm install -g verdaccio
      複製代碼
    2. 啓動verdaccio緩存

      啓動成功後,打開http://localhost:4873/,看到界面就表示成功了bash

      chenwentaodeiMac:ceair_wallet chenwentao$ verdaccio
       warn --- config file  - /Users/chenwentao/.config/verdaccio/config.yaml  //配置文件
       warn --- Plugin successfully loaded: htpasswd //保存用戶帳戶、密碼等信息
       warn --- Plugin successfully loaded: audit
       warn --- http address - http://localhost:4873/ - verdaccio/3.11.4 //地址
      複製代碼
  3. 配置文件

    默認的配置文件容許全部的用戶擁有任何的權限。

    #
    # 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 存儲npm包的路徑
    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 
      # web頁面的配置 即上面的http://localhost:4873/ 默認爲可訪問。title就是標題,能夠修改
      #enable: false
      title: Verdaccio
    
    auth:
    # 保存用戶帳戶、密碼等信息文件,能夠將max_users設置爲-1禁止用戶添加,從而經過修改htpasswd來添加用戶
      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
    # 訪問公共庫的路徑,能夠修改爲淘寶鏡像 https://registry.npm.taobao.org
    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}
    # 配置以後相同wifi下其餘電腦也能夠訪問了 訪問地址爲你的ip加上端口4873
    listen: 0.0.0.0:4873
    
    複製代碼
  4. 客戶端配置

    本地的私有倉庫已經搭建好了,接下來咱們須要經過客戶端配置registry來使用咱們的私有倉庫。在瀏覽器中打開http://10.68.18.154:4873/時,會有提示(10.68.18.154是本機的IP地址)

    Login:

    npm adduser --registry  http://10.68.18.154:4873
    複製代碼

    Publish:

    npm publish --registry http://10.68.18.154:4873
    複製代碼

在linux服務器上嘗試一下

剛纔,咱們在本地構建了一個npm私有庫,如今咱們到Linux服務器上嘗試一下吧。首先,檢測一下有沒有安裝node和python,若是沒有安裝就進行安裝,那麼咱們接下來來安裝一下。

  1. 安裝python

    在Linux上安裝python,須要用命令行去操做。

    下載

    解壓

  2. 安裝node

    下載

    解壓

    接下來和本地同樣去建立npm私有庫,建立完以後讓咱們永久的運行verdaccio吧。

  3. 永久運行verdaccio

    sudo npm install -g forever
    forever start `which verdaccio`
    複製代碼
相關文章
相關標籤/搜索