如何使用npm打包发布nodejs程序包

不论基于任何语言开发内部项目,我们经常会在内部封装一些极具通用性的功能模块。如果我们觉得该通用模块对其它团队有可取之处,完全可以将其开源出去。npm就是发布nodejs程序包的最优工具。

1. 首先在npmjs.org注册一个账号:(可省)

2. 通过npm adduser来注册新账号或登录老账号:

# npm adduser
Username: billfeller
Password: 
Email: (this IS public) 531958936@qq.com

3. 在本地通过npm init完成相应的模块的开发,请注意在main文件中规范对外暴露的接口,注意,关于npm init的用法请参考之前的文章,这里不再详细说明:

4. 完成模块开发后就可以通过npm publish打包发布相应的程序包:

# npm publish
+ uv-tj@1.0.0

关于npm publish的使用请见:

5. 此时你应该可以在npmjs.org上查到uv-tj包:


6. 接下来就可以像使用其它包一样使用uv-tj包了:

# git clone [email protected]:billfeller/uv-tj-demo.git
Initialized 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 init
This 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 fields
and exactly what they do.
 
Use `npm install <pkg> --save` afterwards to install a package and
save 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 demo
author: billfeller
license: (ISC) MIT
About 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 --save
uv-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

此时通过浏览器访问:


完整代码请见:

7. 也可以直接git clone https://github.com/billfeller/uv-tj-demo到本地,直接执行npm install来测试:

# git clone https://github.com/billfeller/uv-tj-demo
Initialized 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]# ls
index.js  LICENSE  package.json  README.md
[root@~/wade/test/uv-tj-demo]# npm install
uv-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

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。