1. npm是Node的模块管理器
可以先了解下NPM的一些基础
2. 创建Node包
打开Node命令行
C:workspace pm-package>mkdir my-package-03
然后使用npm init 命令,会创建一个package.json 文件
{
"name": "my-package-03",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo "Error: no test specified" && exit 1"
},
"author": "larry",
"license": "ISC"
}
还需要一个入口文件 index.js
exports.printMsg = function () {
console.log("this is my first message");
};
3. 发布到npm服务器上
3.1 首先要注册一个npm账户
注意: 注册公共后,邮箱要经过验证,否则只会的publish会报错,邮箱为验证

3.2 登录npm账户
npm login
输入刚才注册的用户名和密码
3.3 发布
npm publish my-package-03
发布时出现的错误
1)only admin can publish

这是因为设置了registry 为淘宝的源,重新设置为npmjs
npm config set registry https://registry.npmjs.org
发布成功后,可重新设置会淘宝
npm config set registry https://registry.npm.taobao.org
2) Please try running this command again as root/Administrator.
这个错误是提示用管理员运行node命令行工具
最终发布成功,显示如下效果图

4. 使用刚才发布的包
创建文件夹 test-my-package,进入test-my-package后,安装my-package-03, 如下图

然后创建index.js 调用test-myckage-03中的方法

最后,运行index.js 文件。 打印出this is my first message

6. 更新npm包
1)修改包的内容
exports.printMsg = function () {
console.log("this is my second message");
};
想first改为second
2)然后修改version
npm version 1.0.1
将版本1.0.0改为1.1.1
3) 再次发布
npm publish