如何创建npm包(模块)并使用

版权声明:如果喜欢,就收藏吧 O(∩_∩)O~ https://blog.csdn.net/caseywei https://blog.csdn.net/caseywei/article/details/87777438

1、编写模块


exports.sayHello =function(){ 
return 'Hello World'; 

保存为index.js

2、初始化包描述文件


$ npm init 
package.json


"name": "wu_xx", 
"version": "1.0.1", 
"description": "wu_xx first demo", 
"main": "index.js", 
"scripts": { 
"test": "make test" 
}, 
"repository": { 
"type": "git", 
"url": "git+https://github.com/blackwuxin/testdemo.git" 
}, 
"keywords": [ 
"demo" 
], 
"author": "wu_xx", 
"license": "ISC", 
"bugs": { 
"url": "https://github.com/blackwuxin/testdemo/issues" 
}, 
"homepage": "https://github.com/blackwuxin/testdemo#readme", 

3、注册npm仓库账号


https://www.npmjs.com 上面的账号 
$ npm adduser

4、上传包


$ npm publish

5、安装包


$ npm install wu_xx

6、管理包权限


查看模块拥有者 
$ npm owner ls <package_name> 
添加一个发布者 
$ npm owner add <user> <package_name> 
删除一个发布者 
$ npm owner rm <user> <package_name>

7、分析包


查看当前项目引用了哪些包 
npm ls

8、使用引入包


var hello = require('wu_xx'); 
hello.sayHello();

参考:http://www.runoob.com/nodejs/nodejs-npm.html

猜你喜欢

转载自blog.csdn.net/caseywei/article/details/87777438