首页 > 代码库 > 如何使用npm打包发布nodejs程序包
如何使用npm打包发布nodejs程序包
不论基于任何语言开发内部项目,我们经常会在内部封装一些极具通用性的功能模块。如果我们觉得该通用模块对其它团队有可取之处,完全可以将其开源出去。npm就是发布nodejs程序包的最优工具。
1. 首先在npmjs.org注册一个账号:(可省)
- https://www.npmjs.org/signup
2. 通过npm adduser来注册新账号或登录老账号:
# npm adduserUsername: billfellerPassword: Email: (this IS public) 531958936@qq.com
3. 在本地通过npm init完成相应的模块的开发,请注意在main文件中规范对外暴露的接口,注意,关于npm init的用法请参考之前的文章,这里不再详细说明:
- 基于express+redis快速实现实时在线用户数统计
4. 完成模块开发后就可以通过npm publish打包发布相应的程序包:
# npm publish+ uv-tj@1.0.0
关于npm publish的使用请见:
- npm-publish
5. 此时你应该可以在npmjs.org上查到uv-tj包:
6. 接下来就可以像使用其它包一样使用uv-tj包了:
# git clone git@github.com:billfeller/uv-tj-demo.gitInitialized empty Git repository in /root/wade/nodejs/uv-tj-demo/.git/remote: Counting objects: 5, done.remote: Compressing objects: 100% (4/4), done.remote: Total 5 (delta 0), reused 0 (delta 0)Receiving objects: 100% (5/5), done.[root@~/wade/nodejs]# cd uv-tj-demo/[root@~/wade/nodejs/uv-tj-demo]# npm initThis utility will walk you through creating a package.json file.It only covers the most common items, and tries to guess sane defaults. See `npm help json` for definitive documentation on these fieldsand exactly what they do. Use `npm install <pkg> --save` afterwards to install a package andsave it as a dependency in the package.json file. Press ^C at any time to quit.name: (uv-tj-demo) version: (1.0.0) description: entry point: (index.js) test command: git repository: (https://github.com/billfeller/uv-tj-demo.git) keywords: uv tj module demoauthor: billfellerlicense: (ISC) MITAbout to write to /root/wade/nodejs/uv-tj-demo/package.json: { "name": "uv-tj-demo", "version": "1.0.0", "description": "uv-tj-demo ==========", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "https://github.com/billfeller/uv-tj-demo.git" }, "keywords": [ "uv", "tj", "module", "demo" ], "author": "billfeller", "license": "MIT", "bugs": { "url": "https://github.com/billfeller/uv-tj-demo/issues" }, "homepage": "https://github.com/billfeller/uv-tj-demo"} Is this ok? (yes) yes[root@~/wade/nodejs/uv-tj-demo]# vim package.json [root@~/wade/nodejs/uv-tj-demo]# npm i uv-tj --saveuv-tj@1.0.0 node_modules/uv-tj├── redis@0.12.1└── express@4.10.2 (utils-merge@1.0.0, merge-descriptors@0.0.2, fresh@0.2.4, cookie@0.1.2, escape-html@1.0.1, range-parser@1.0.2, cookie-signature@1.0.5, finalhandler@0.3.2, vary@1.0.0, media-typer@0.3.0, methods@1.1.0, parseurl@1.3.0, serve-static@1.7.1, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.0, qs@2.3.2, etag@1.5.0, on-finished@2.1.1, debug@2.1.0, proxy-addr@1.0.3, send@0.10.1, accepts@1.1.3, type-is@1.5.3)[root@~/wade/nodejs/uv-tj-demo]# vim index.js[root@~/wade/nodejs/uv-tj-demo]# npm start > uv-tj-demo@1.0.0 start /root/wade/nodejs/uv-tj-demo> node index.js
此时通过浏览器访问:
完整代码请见:
- https://github.com/billfeller/uv-tj-demo
7. 也可以直接git clone https://github.com/billfeller/uv-tj-demo到本地,直接执行npm install来测试:
# git clone https://github.com/billfeller/uv-tj-demoInitialized empty Git repository in /root/wade/test/uv-tj-demo/.git/remote: Counting objects: 9, done.remote: Compressing objects: 100% (7/7), done.remote: Total 9 (delta 1), reused 4 (delta 0)Unpacking objects: 100% (9/9), done.[root@~/wade/test]# cd uv-tj-demo/[root@~/wade/test/uv-tj-demo]# lsindex.js LICENSE package.json README.md[root@~/wade/test/uv-tj-demo]# npm installuv-tj@1.0.0 node_modules/uv-tj├── redis@0.12.1└── express@4.10.2 (merge-descriptors@0.0.2, utils-merge@1.0.0, fresh@0.2.4, cookie@0.1.2, escape-html@1.0.1, range-parser@1.0.2, cookie-signature@1.0.5, finalhandler@0.3.2, vary@1.0.0, media-typer@0.3.0, methods@1.1.0, parseurl@1.3.0, serve-static@1.7.1, content-disposition@0.5.0, path-to-regexp@0.1.3, depd@1.0.0, qs@2.3.2, on-finished@2.1.1, etag@1.5.0, debug@2.1.0, type-is@1.5.3, proxy-addr@1.0.3, send@0.10.1, accepts@1.1.3)[root@~/wade/test/uv-tj-demo]# npm start > uv-tj-demo@1.0.0 start /root/wade/test/uv-tj-demo> node index.js
如何使用npm打包发布nodejs程序包
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。