因为业务安全需要等种种原因,不能够把插件都发布到公共的npm仓库,所以需要搭建自己的私有npm仓库,最近自己搭建了一个简单的npm仓库,踩了些坑,和大家分享一下,希望能够帮到有需要的童鞋本次讲述用sinopia从0开始搭建npm仓库,讲得会比较细,觉得啰嗦的童鞋可以只看自己需要的哈([原文链接[1])服务器端安装nodejs和npm因为我的linux服务器是centos的,所以我这边讲的是一种在centos上安装node 环境1.安装wgetyum install -y wget2.下载最新的node包可以在nodejs的官网找到最新的下载地址(找到需要的,右键复制链接地址就可以拿到最新的地址啦)wget https://nodejs.org/en/download/node-v10.15.0-linux-x64.tar.xz解压安装包xz -d node-v10.15.0-linux-x64.tar.xztar -xf node-v10.15.0-linux-x64.tar.xz部署bin文件重点是要找到你的nodejs的文件路径(你将node文件解压到哪里就是哪里。),找不到node路径的童鞋请执行whereis node然后执行ln -s node路径 /usr/bin/nodeln -s node路径 /usr/bin/npmeg://我的node解压路径为/opt/node-v10.15.0-linux-x64/bin/nodeln -s /opt/node-v10.15.0-linux-x64/bin/node /usr/bin/nodeln -s /opt/node-v10.15.0-linux-x64/bin/node /usr/bin/npm如果出现ln: failed to create symbolic link ‘/usr/bin/node’: File exists执行:rm /usr/bin/node查看是否安装成功node -vnpm -v搭建npm仓库sinopia安装装好node以后,我们就可以在服务器直接安装sinopia了,一行命令全局安装npm install -g sinopia 安装好了可以启动试一下sinopia出现下面的结果说明成功跑起来了,这个时候可以打开http://localhost:4873/访问了Sinopia doesn’t need superuser privileges. Don’t run it under root. warn — config file - /root/.config/sinopia/config.yaml warn — http address - http://localhost:4873/如果报sinopia: command not found,请添加关联,找到你的sinopia路径(这个路径在之前node路径的子目录里面)ln -s /opt/node-v10.15.0-linux-x64/bin/sinopia /usr/binpm2node服务非常脆弱,一般在实际中使用都会配合守护进程。这里我用的是 pm2 做守护进程安装npm install -g pm2pm2 -v如果出现 pm2: command not found,和sinopia的方法一样ln -s /opt/node-v10.15.0-linux-x64/bin/pm2 /usr/bin通过 PM2 启动 sinopia:pm2 start which sinopia结果类似如下:┌─────────┬────┬──────┬────────┬────────┬─────┬────────┬───────────┐│ Name │ id │ mode │ status │ ↺ │ cpu │ memory │├─────────┼────┼──────┼────────┼────────┼─────┼────────┼───────────┤│ sinopia │ 0 │ N/A │ fork │ online │ 0 │ 0% │ 16.7 MB │└─────────┴────┴──────┴────────┴────────┴─────┴────────┴───────────┘sinopia配置修改默认情况下,sinopia 的配置是不适合直接使用的,所以我们需要对它的配置文件按需酌情修改我们找到上面提到的配置文件目录,打开配置文件进行编辑## 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 packagesstorage: ./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 touplinks: npmjs: url: http://registry.npm.taobao.org/ #默认为npm的官网,由于国情,修改 url 让sinopia使用 淘宝的npm镜像地址 packages: #配置权限管理 ‘@/’: # scoped packages access: $all #表示哪一类用户可以对匹配的项目进行安装 【$all 表示所有人都可以执行对应的操作,$authenticated 表示只有通过验证的人可以执行对应操作,$anonymous 表示只有匿名者可以进行对应操作(通常无用)】 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 #如其名,这里的值是对应于 uplinks# log settingslogs: - {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 #默认没有,只能在本机访问,添加后可以通过外网访问==listen: 0.0.0.0:4873 这一条一定得加,然后记得把服务器的4873的端口开放==访问在自己的电脑浏览器上输入服务器ip地址+端口号,端口号默认为4873eg:192.186.1.343:4873到这里,我们可以成功的在自己的服务器上搭建我们的私有npm啦~~