使用sinopia构建npm中转站

2018-06-04  本文已影响23人  去年17
使用sinopia这个工具,主要是做为一个内网虚拟机的中转站,具体是因为开发需求不能在虚拟机内访问internet,那么,我要加载node模块怎么办,当然sinopia不仅只是做一个中转站,还可以发布自己的私人node包,这个也有助于内网使用。

说下原理:

npm请求流程

1、npmjs.com
2、npm.taobao.com
3、可访问互联网的内网server(安装sinopia)
4、client(请求node包)

具安装步骤:

1、安装node 环境(建议8.x 因为使用10.x的时候有sinopia报错)
... 省略

2、npm下载sinopia

$ npm install -g sinopia 
//或者使用cnpm(使用taobao镜像工具) **推荐**
$ npm install -g cnpm --registry=https://registry.npm.taobao.org
$ cnpm install -g sinopia 

3、配置 sinopia
参考自 #使用Sinopia搭建私有的npm仓库

#
# 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
    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  #//默认没有,只能在本机访问,添加后可以通过外网访问。

4、启动 sinopia
sinopia

$ sinopia  

5、客户端配置
参考自国内npm镜像及配置方法

//注册ip
npm config set registry http://192.168.1.200:4873
// 配置后可通过下面方式来验证是否成功
npm config get registry
// 或
npm info express
上一篇下一篇

猜你喜欢

热点阅读