搭建私有npm仓库

这里使用 Sinopia 搭建npm仓库。只需两句代码即可搭建成功。

sinopia有以下几个优势:

  1. 不同步拉取npm库,占据大量硬盘,没有硬盘被撑爆的问题;
  2. 安装配置极其简单,不需要数据库;
  3. 支持配置上游registry配置,一次拉取即缓存;
  4. 直接可以拉取docker镜像;

安装

$ npm install -g sinopia
$ sinopia

curl http://localhost:4873/,没有timeout。这时已经搭好了。不过只能本地访问,可以直接修改config.yaml配置文件,最后一行listen: 0.0.0.0:4873。不过推荐nginx反向代理到80。当然还可以通过docker搭建,也很简单。

使用pm2守护进程

pm2 start `which sinopia`

config.yaml配置文件

~/.config/sinopia/目录。

# 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
    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}

# you can specify listen address (or simply a port) 
listen: 0.0.0.0:4873  ////默认没有,只能在本机访问,添加后可以通过外网访问。

客户端使用

安装nrm

$ npm install -g nrm # 安装nrm
$ nrm add XXXXX http://XXXXXX:4873 # 添加本地的npm镜像地址
$ nrm use XXXX # 使用本址的镜像地址


ldh@ldh-PC:~/GitHubs/aa$ 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/
* lnpm --- http://npm.dadigua.win/

已经直接使用私有仓库就行了。如果找不到库,会自动到上游仓库去拉的。然后npm addusernpm loginnpm publish 等命令sinopia搭建的npm库都是支持的。

如下是Sinopia搭建npm库的首页。publish了一个库测试了下。