前端人员如何在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`
    复制代码
相关文章
相关标签/搜索