一、搭建CNPM
关于搭建CNPM的方法可以参考本站:http://www.16boke.com/article/detail/155
二、安装第三方npm库的模块
npm install async --registry=http://192.168.48.57:7001 # or npm config set registry http://192.168.48.57:7001 npm install [packname] 其中http://192.168.48.57:7001为私有npm安装地址
三、发布私有模块
1、创建node项目(模块)
mkdir server cd server/ npm init 根据提示输入相应的值,也可以一路回车
这样就创建好了一个简单的node模块,必须包含package.json
2、注册管理员账户
执行npm adduser命令,根据提示输入用户名、密码、邮箱。
其中用户名为cnpm安装目录中/cnpmjs.org/config/index.js文件中
admins: { // name: email admin: 'demo@163.com' },
其中用户名为admin,密码为admin,邮箱为demo@163.com
3、发布项目
进入server这个node项目的根目录中,执行:
npm publish --regirsty=http://192.168.48.57:7001
一般会报错:
是因为cnpm配置文件index.js中配置的有:
所以需要在模块名加上上面三个中的一个即修改package.json:
{ "name": "@cnpm/server", "version": "1.0.0", "description": "server", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC" }
修改完后再次publish:
4、下载新提交的模块
下载刚才提交到私有仓库中的server模块,使用下面命令:
npm install server
5、程序中引用
在程序中引用的时候需要使用server=require('@cnpm/server')。
注意:
1、可以自定义scope,只需要修改/cnpmjs.org/config/index.js文件中scopes:[],在里面添加自己项目组的名称。