文章目录

  • 前言
  • 安装
  • 进程守卫
  • 添加账号
  • 上传
  • 下载


前言

随着前端工程的日益扩大,一个成熟的团队都需要有npm私服。所以搭建一个npm私有仓库是基本的团队建设。搭建私服有很多种方法,这里我们讲解一下如何利用sinopia搭建。下次讲解使用nexus搭建npm私服。nexus很强大,也可以搭建maven私服。

安装

安装sinopia

[root@AlexWong opt]# npm i sinopia -g
# 直接启动
[root@AlexWong opt]# sinopia
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/
# 修改配置文件添加以下内容支持外网访问
[root@AlexWong opt]# vim /root/.config/sinopia/config.yaml
# 外网访问
listen: 0.0.0.0:4873

进程守卫

或者采用pm2进行进程守护, 这也就可以后台执行了

[root@AlexWong opt]# pm2 start sinopia
[PM2] Starting /opt/node/bin/sinopia in fork_mode (1 instance)
[PM2] Done.
┌────┬────────────────────┬──────────┬──────┬───────────┬──────────┬──────────┐
│ id │ name               │ mode     │ ↺    │ status    │ cpu      │ memory   │
├────┼────────────────────┼──────────┼──────┼───────────┼──────────┼──────────┤
│ 0  │ sinopia            │ fork     │ 0    │ online    │ 0%       │ 18.8mb   │
└────┴────────────────────┴──────────┴──────┴───────────┴──────────┴──────────┘

到此为止,显示安装成功了,记得在安全组放开4873端口

npm 使用私用仓库 nexus搭建npm私有仓库_ios

/root/.config/sinopia/config.yaml是配置文件所在;

[root@AlexWong opt]# cat /root/.config/sinopia/config.yaml
#
# 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

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: 1000

# a list of other known repositories we can talk to
uplinks:
  npmjs:
    url: https://registry.npmjs.org/

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

# 外网访问
listen: 0.0.0.0:4873

# log settings
logs:
  - {type: stdout, format: pretty, level: http}
  #- {type: file, path: sinopia.log, level: info}

配置文件目录:

  • config.yaml    ------sinopia的配置文件
  • htpasswd     ------存放用户账户信息的文件,密码通过sha1、base64加密
  • storage     ------存放npm包及缓存包的文件夹

添加账号

注册私服的用户

首先我们需要切换的我们npm源,这里建议使用nrm进行多源管理,如果有不清楚的可以去看我的文章 nrm多源头管理

[root@AlexWong opt]# npm i nrm -g
[root@AlexWong opt]# nrm ls

* npm -------- https://registry.npmjs.org/
  yarn ------- https://registry.yarnpkg.com/
  cnpm ------- http://r.cnpmjs.org/
  taobao ----- https://registry.npm.taobao.org/
  nj --------- https://registry.nodejitsu.com/
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/

[root@AlexWong opt]# nrm add my http://124.*.*.25:4873/

    add registry my success

[root@AlexWong opt]# nrm ls

* npm -------- https://registry.npmjs.org/
  yarn ------- https://registry.yarnpkg.com/
  cnpm ------- http://r.cnpmjs.org/
  taobao ----- https://registry.npm.taobao.org/
  nj --------- https://registry.nodejitsu.com/
  npmMirror -- https://skimdb.npmjs.com/registry/
  edunpm ----- http://registry.enpmjs.org/
  my --------- http://124.*.*.25:4873/

[root@AlexWong opt]# nrm use my
                        

   Registry has been set to: http://124.*.*.25:4873/

[root@AlexWong opt]# nrm current
my

# 注册用户
[root@AlexWong opt]# npm adduse
Username: admin
Password: 
Email: (this IS public) alex@gmail.com
Logged in as admin on http://124.*.*.25:4873/.

npm 使用私用仓库 nexus搭建npm私有仓库_前端架构_02


npm 使用私用仓库 nexus搭建npm私有仓库_前端架构_03

上传

新建test项目

npm 使用私用仓库 nexus搭建npm私有仓库_npm私有库_04


npm 使用私用仓库 nexus搭建npm私有仓库_npm 使用私用仓库_05

{
  "name": "test",
  "version": "1.0.1",
  "description": "test component",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "publishConfig": {
    "registry": "http://124.*.*.25:4873/"
  },
  "author": "alex",
  "license": "ISC"
}

除了package.json指定publishConfig还可以新建.npmrc文件 指定私有仓库

alex:test $ touch .npmrc
alex:test $ vim .npmrc
registry=http://124.*.*.25:4873/

上传

alex:test $ npm login
alex:test $ npm publish
npm notice
npm notice 📦  test@1.0.0
npm notice === Tarball Contents ===
npm notice 0B   .history/.npmrc_20210515224849
npm notice 9B   .history/.npmrc_20210515225023
npm notice 36B  .history/.npmrc_20210515225029
npm notice 37B  .history/.npmrc_20210515225032
npm notice 37B  .history/.npmrc_20210515225037
...
+ test@1.0.2

npm 使用私用仓库 nexus搭建npm私有仓库_私有库_06

下载

新建test2项目

alex:test $ mkdir test2 && cd test2 && npm init
# 切换到私有库
alex:test $ nrm use my
# 下载
alex:test $ npm install test --save

npm 使用私用仓库 nexus搭建npm私有仓库_前端架构_07


问题来了,如果我不想把当前库的来源切换为私有库呢,是否可以直接下载使用?

方法1: 指定私有库地址路径
格式化为 "test": "http://你的IP:4873/你的包名/-/你的包名-版本号.tgz"

{
  "name": "test2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.21.1",
    "test": "http://124.*.*.25:4873/test/-/test-1.0.2.tgz"
  }
}

方法2: 使用.npmrc

alex:test2 $ touch .npmrc
alex:test2 $ vim .npmrc
registry=http://124.*.*.25:4873/
{
  "name": "test2",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^0.21.1",
    "test": "^1.0.2"
  }
}

第二种方法就不需要经常切换来源了。其实如第一种也不用切换回来到npmjs。如果当前私有库查找不到对应包的存在,依然会转发到npmjs进行下载。并且会保存到私有库里面。我们去服务器查看下storage:

[root@AlexWong opt]# ls /root/.config/sinopia/storage
drwxr-xr-x 2 root root 4096 May 15 23:57 axios
drwxr-xr-x 2 root root 4096 May 15 23:57 follow-redirects
drwxr-xr-x 2 root root 4096 May 15 23:57 test

我们再基于私有库下载一个私有库不存在的vue包.

alex:test2 $ npm i vue --save
[root@AlexWong opt]# ls /root/.config/sinopia/storage
drwxr-xr-x 2 root root 4096 May 15 23:57 axios
drwxr-xr-x 2 root root 4096 May 15 23:57 follow-redirects
drwxr-xr-x 2 root root 4096 May 15 23:57 test
drwxr-xr-x 2 root root 4096 May 15 23:57 vue

问题得到验证。下载不属于当前私有库的包,也会保存在私有库。