搭建npm私有库

1.verdaccio

搭建npm的私有库得部署个服务用来存放编写的库代码,verdaccio是常用的用来部署此服务的工具。这里使用本机作为示例,实际使用中一般是找台公司的内网服务器。
全局安装

npm install -g verdaccio

安装完成后执行

verdaccio

即可自动在浏览器中访问 http://localhost:4837
配置文件在为:/Users/用户/.config/verdaccio/config.yaml,在其最后一行添加

#默认是没有的,只能在本机访问,添加后可以通过外网访问
listen: 0.0.0.0:4873

实际工作中verdaccio应被部署在内网服务器。

2.pm2

PM2 是一个带有负载均衡功能的 Node 应用进程管理器。verdaccio运行起来后,会在关闭掉terminal对话框后也停止运行,所以需要PM2来“守护进程”。
全局安装

npm install -g pm2

pm2 -v // 查看是否安装成功
5.3.0

使用pm2运行verdaccio,成功后即可打开http://localhost:4837查看

pm2 start verdaccio

│ id │ name         │ namespace   │ version │ mode    │ pid      │ uptime │ ↺    │ status    │ cpu      │ mem      │ user     │ watching │
├────┼──────────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0  │ verdaccio    │ default     │ N/A     │ fork    │ 56899    │ 78s    │ 0    │ online    │ 0%       │ 39.8mb   │ userName │ disabled │

停止verdaccio

pm2 stop verdaccio

3.nrm

nrm是一个对npm源方便进行增删改查的工具,建议开发人员都应在本机安装。
本机全局安装

npm install -g nrm

查看已有源

nrm ls

// npm ---------- https://registry.npmjs.org/
// yarn --------- https://registry.yarnpkg.com/
// tencent ------ https://mirrors.cloud.tencent.com/npm/
// cnpm --------- https://r.cnpmjs.org/
// taobao ------- https://registry.npmmirror.com/
// npmMirror ---- https://skimdb.npmjs.com/registry/

新增verdaccio本地源

// 实际工作中地址换成内网服务器地址
nrm add yourRegistryName http://localhost:4873

查看

nrm ls

// npm --------------- https://registry.npmjs.org/
// yarn -------------- https://registry.yarnpkg.com/
// tencent ----------- https://mirrors.cloud.tencent.com/npm/
// cnpm -------------- https://r.cnpmjs.org/
// taobao ------------ https://registry.npmmirror.com/
// npmMirror --------- https://skimdb.npmjs.com/registry/
// yourRegistryName -- http://localhost:4873/          // 添加成功

4.写代码

cd firstNpm  // 位移到空文件夹下

npm init     // 创建package.json

新建index.js

// 默认引用库时是引用的index.js文件。
module.exports.hello = function() {
  console.log('hello world')
}

修改package.json

{
  "name": "firstNpm",
  "version": "0.0.1",
  "description": "第一个私有库",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "acker",
  "license": "ISC"
}

5.发布

nrm use yourRegistryName 

nrm ls
//  npm --------------- https://registry.npmjs.org/
//  yarn -------------- https://registry.yarnpkg.com/
//  tencent ----------- https://mirrors.cloud.tencent.com/npm/
//  cnpm -------------- https://r.cnpmjs.org/
//  taobao ------------ https://registry.npmmirror.com/
//  npmMirror --------- https://skimdb.npmjs.com/registry/
//* yourRegistryName -- http://localhost:4873/

npm publish                           // 发布
// npm unpublish firstNpm@0.0.1       // 撤销发布的库(不指定版本即撤销掉整个firstNpm库)

如果成功的话在http://localhost:4873/就可以看到刚刚上传的firstNpm

6.优化

.npmrc

即npm running cnfiguration, npm配置文件。其可以设置package.json中依赖包的安装来源,既从哪里下载依赖包。
.npmrc在电脑中有多份,优先级由高到低为:

项目配置文件: /project/.npmrc
用户配置文件:~/.npmrc
全局配置文件:$PREFIX/etc/npmrc
npm 内置配置文件 /path/to/npm/npmrc

工作中可在每个项目下创建.npmrc文件

registry=https://registry.npmjs.org/       // 普通库从公有库下载
@company:registry=http://localhost:4873/   // 以@company开头的库从私有库下载

故公司私有库可统一前缀@company/firstNpm_xxx

{
  "name": "@company/firstNpm_xxx", // 统一前缀
  "version": "0.0.1",
  "description": "第一个私有库",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "acker",
  "license": "ISC"
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容