使用verdaccio完成npm私服搭建

2,193 阅读2分钟

参考《史上最贴心NPM私服搭建辅导》文章内容,使用verdaccio完成npm私服搭建,在此感谢。

搭建环境:
  • 云服务器ECS一台(实例)
  • 本地电脑

本地电脑使用secureCRT远程连接(SSH2)云服务器实例

1. 安装verdaccio

# 全局安装verdaccio
[root@iZwz960f904j96nggfchxpZ ~]# npm install -g verdaccio

# 建立软链,否则提示command not found
[root@iZwz960f904j96nggfchxpZ ~]# ln -s ~/node-v10.15.1-linux-x64/bin/verdaccio /usr/local/bin/

# verdaccio安装成功
[root@iZwz960f904j96nggfchxpZ ~]# verdaccio -h
Usage: verdaccio [options]

Options:
  -i, --info                  prints debugging information about the local environment
  -l, --listen <[host:]port>  host:port number to listen on (default: localhost:4873)
  -c, --config <config.yaml>  use this configuration file (default: ./config.yaml)
  -V, --version               output the version number
  -h, --help                  output usage information
  
[root@iZwz960f904j96nggfchxpZ ~]# verdaccio -V
4.9.1

2. 运行verdaccio

[root@iZwz960f904j96nggfchxpZ ~]# verdaccio         
 warn --- config file  - /root/.config/verdaccio/config.yaml
 warn --- Verdaccio started
 warn --- Plugin successfully loaded: verdaccio-htpasswd
 warn --- Plugin successfully loaded: verdaccio-audit
 warn --- http address - http://localhost:4873/ - verdaccio/4.9.1

如在本地搭建,通过浏览器访问http://localhost:4873,查看npm包管理网页
如在云服务器搭建,本人是配置了nginx,将外网80端口映射至内网4873端口

3. 配置nginx

在创建/etc/nginx/conf.d创建verdaccio-nginx.conf,内容如下:

upstream verdacciodemo {
    server 127.0.0.1:4873;
    keepalive 64;
}

server {
    listen 80;
    server_name 你的服务器公网ip地址或域名;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_set_header X-Nginx-Proxy true;
        proxy_set_header Connection "";
        proxy_pass http://verdacciodemo;
    }
}
# 重启nginx
[root@iZwz960f904j96nggfchxpZ ~]# nginx -s reload

本地电脑浏览器访问http://xxx.xxx.xxx(你的云服务器ip),即可看见verdaccio搭建的npm管理页面,如下图所示:

4. 配置verdaccio

修改~/.config/verdaccio/config.yaml, 只改了packages部分,其他不变,内容如下:

  ...
  
packages:
  '@kuiper/*':
    access: $all
    publish: $authenticated
    unpublish: $authenticated 
  '@*/*':
    # scoped packages
    access: $all
    publish: $authenticated
    unpublish: $authenticated
    proxy: npmjs
    
    ...

以上增加了'@kuiper/*'配置,如果你发布的包是 @kuiper 前缀的,那就表明是私有包,不会代理到外部。

5. 准备npm包

npm包如何开发这里就不赘述了,结构如下图所示:

6. 上传npm包到私服

上传步骤可参考verdaccio管理页面中的提示:

# 登录并创建用户名/密码/邮箱
MacBook-Pro:~ kuiper$ npm adduser --registry http://你的云服务器ip
Username: hello
Password: 
Email: (this IS public) 

# 进入@kuiper/firstdemo目录,发布npm包
MacBook-Pro:firstdemo kuiper$ npm publish --registry http://你的云服务器ip
npm notice 
npm notice 📦  @kuiper/firstdemo@1.0.1
npm notice === Tarball Contents === 
npm notice 297.9kB index.js    
npm notice 297B    package.json
npm notice 45B     README.md   
npm notice === Tarball Details === 
npm notice name:          @kuiper/firstdemo                       
npm notice version:       1.0.1                                   
npm notice package size:  86.7 kB                                 
npm notice unpacked size: 298.2 kB                                
npm notice shasum:        5b0a9006562241ed4e501cb4acc11905e7c193ee
npm notice integrity:     sha512-SragAVP+mldRF[...]6Ir2FTu0lfgUg==
npm notice total files:   3                                       
npm notice 
+ @kuiper/firstdemo@1.0.1

7. 查看刚上传的npm包

访问http://xxx.xxx.xxx(你的云服务器ip),如下图所示:

8. 使用pm2部署verdaccio

笔者使用secureCRT连接云服务器,如终端窗口关掉,verdaccio服务将无法访问,使用pm2可解决上述问题。

[root@iZwz960f904j96nggfchxpZ ~]# pm2 start verdaccio
[PM2] Applying action restartProcessId on app [verdaccio](ids: 0)
[PM2] [verdaccio](0) ✓
[PM2] Process successfully started
┌─────┬──────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name         │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ verdaccio    │ default     │ N/A     │ fork    │ 10825    │ 0s     │ 0    │ online    │ 0%       │ 21.2mb   │ root     │ disabled │
└─────┴──────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

# 查看pm2管理的服务
[root@iZwz960f904j96nggfchxpZ ~]# pm2 list
┌─────┬──────────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id  │ name         │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├─────┼──────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0   │ verdaccio    │ default     │ N/A     │ fork    │ 11937    │ 0s     │ 101  │ online    │ 0%       │ 76.6mb   │ root     │ disabled │
└─────┴──────────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘

9. 本地安装私服上的npm包

# 本地安装私服npm包,注意配置registry
MacBook-Pro:test kuiper$ npm install @kuiper/firstdemo --registry=http://你的云服务器ip
+ @kuiper/firstdemo@1.0.1
updated 1 package and audited 2325 packages in 8.889s

75 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities

如不指定--registry=http://xxx.xxx.xx,则会报404错误:Not Found - GET https://registry.npmjs.org/@kuiper%2ffirstdemo - Not found