准备工作

Node.js >= 4.2.3
Linux or OSX

通过npm安装cnpmjs.org和cnpm

$ npm install -g --build-from-source cnpmjs.org cnpm sqlite3

此处使用的是sqlite3数据库

启动cnpmjs.org服务

  • admins: myname,othername
  • scopes: my-company-name,other-name
  • default ports: 7001-registry, 7002-web

$ nohup cnpmjs.org start --admins='myname,othername' \ --scopes='@my-company-name,@other-name' &

修改cnpm的默认镜像

$ cnpm set registry http://localhost:7001

默认镜像可以先安装nrm,然后通过切换镜像实现。

1
2
3
4
5
$ npm i -g nrm

$ nrm add private 'http://localhost:7001'

$ nrm use private

使用cnpm登录你的私有镜像

1
2
3
4
$ cnpm login
Username: myname
Password: ***
Email: (this IS public) m@fengmk2.com

发布私有包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
$ cd /tmp
$ mkdir helloworld && cd helloworld
$ cnpm init
name: (helloworld) @my-company-name/helloworld
version: (1.0.0)

{
"name": "@my-company-name/helloworld",
"version": "1.0.0",
"description": "my first scoped package",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC"
}

$ cnpm publish
+ @my-company-name/helloworld@1.0.0

使用 cnpm init --scope my-company-name来实现私有包的初始化。

查看私有包

使用浏览器查看

$ open http://localhost:7002/@my-company-name/helloworld

或者cnpm info

$ cnpm info

所有公用包都可以通过cnpm安装成功

$ cnpm install mocha

补充

cnpm相关配置可以在 ~/.cnpmjs.org/config.json 中修改。如下

详细配置可参考: https://github.com/cnpm/cnpmjs.org/blob/master/config/index.js

1
2
3
4
5
6
7
8
9
10
{
"bindingHost": "0.0.0.0", // 配置0.0.0.0, 允许外网可以访问
"admins": {
"dani": "dani@localhost.com"
},
"scopes": [
"@dani"
],
"registryHost": "0.0.0.0:7001"
}

私有包都需要带上scope,如果不想手动修改package.json的内容。可以使用 cnpm init –scope my-company-name自动生成

带上scope

地址

cnpmjs.org
Deploy a private npm registry in 5 minutes