注册一个npm账号
- 进入网址: https://www.npmjs.com 注册一个npm账号。
- 进邮箱验证npm账号(否则发布会报错)
开发一个npm包(举例)
- 新建文件夹daisy-npm-test
- 新建文件a.js
function hello(name){
console.log("hello "+ name);
}
exports.hello=hello;
新建文件b.js
var h=require('./a');
h.hello('Jarrick');
npm init
npm init
npm adduser
adduser
npm publish
npm publish.png
发布后可在自己的npm主页看到
3.项目中应用npm包
cnpm install daisy-npm-test
install.png
使用刚才发布的包:
跟使用普通的npm包一样,创建一个index.js,
let a = require('daisy-npm-test')
a.hello('daisy')
执行node index即可看见输出了hello daisy
更新npm包
如果之后修改过此包,需要修改package.json中的版本号字段version,使其大于当前版本,然后npm publish即可。
如果未更改版本号,会报错:
npm ERR! publish Failed PUT 403
npm ERR! code E403
npm ERR! You cannot publish over the previously published version 1.0.0. : qzy-npm-test
npm ERR! A complete log of this run can be found in:
npm ERR! C:\Users\quanzaiyu\AppData\Roaming\npm-cache\_logs\2017-09-12T07_59_18_829Z-debug.log
修改版本号后则成功:
npm publish
- daisy-npm-test@1.0.1
删除npm包
npm unpublish
npm unpublish --force