大致流程就是先创建项目,完善代码,然后发布到git公共仓库,再发布到npm市场
1、新建一个文件夹:mkdir npm_test,并进入该目录:cd npm_test/
![](https://img.haomeiwen.com/i3341870/5a97a0e41af26bed.png)
2、新建一个a.js文件,并编辑代码
function hello(name){
console.log('hello '+name);
}
exports.hello = hello;
3、再新建一个b.js,并编辑代码:
var h = require('./a');
h.hello('fooke');
4、用node测试我们刚刚写的代码:node b.js,可以看到输出
![](https://img.haomeiwen.com/i3341870/77c406dbc05bc55a.png)
5、我们可以把项目上传到GitHub托管了:
5.1登录并新建一个项目目录:这里仅测试使用,就叫printname,得到git地址
5.2把本地这个项目上传到GitHub仓库
![](https://img.haomeiwen.com/i3341870/67a5e19c92960e7f.png)
会提示我们输入git账号密码,然后就上传成功
6、发布到npm市场
6.1 npm init
6.2 输入项目name:printname,版本号:0.0.1,描述description:print something test,entry point :a.js,git repository: (https://github.com/xxxxxxx/printname.git),keyword:搜索的关键字,author:作者,然后保存
![](https://img.haomeiwen.com/i3341870/0526f694c95fcfba.png)
6.3 npm publish发布
6.5,去npm官网搜索查看我们刚刚上传的项目
![](https://img.haomeiwen.com/i3341870/c5c6fa99fc64980d.png)
7、在node.js项目中require验证
7.1先执行:npm install --dev printname
7.2新建一个testNpm.js文件:
var testPrintname = require('printname')
testPrintname.hello('somethings')
7.3node testNpm.js 就能看到输出hello somethings
![](https://img.haomeiwen.com/i3341870/76bd596c8eb62483.png)
网友评论