手把手教你用npm 制作npm库并发布到npm市场

2018-01-12  本文已影响359人  fooke

准备工作:先创建GitHub账户和npm账户

大致流程就是先创建项目,完善代码,然后发布到git公共仓库,再发布到npm市场

1、新建一个文件夹:mkdir npm_test,并进入该目录:cd npm_test/

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,可以看到输出

5、我们可以把项目上传到GitHub托管了:

5.1登录并新建一个项目目录:这里仅测试使用,就叫printname,得到git地址

5.2把本地这个项目上传到GitHub仓库

会提示我们输入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:作者,然后保存

6.3 npm publish发布

6.5,去npm官网搜索查看我们刚刚上传的项目

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 

上一篇 下一篇

猜你喜欢

热点阅读