在npm上发布你的Node模块
3ks 华雷
第一步:
注册npm帐号:https://npmjs.org/
第二步:
生成必要模块描述文件package.json ,readme
package.json详解
{"name": "http-server","preferGlobal": "true","version": "0.3.0","author": "Nodejitsu <support@nodejitsu.com>","description": "a simple zero-configuration command-line http server","contributors": [
{"name": "Marak Squires","email": "marak@nodejitsu.com"}],"bin": {
"http-server": "./bin/http-server"},"scripts": {
"start": "node ./bin/http-server","test": "vows --spec --isolate","predeploy": "echo This will be run before deploying the app","postdeploy": "echo This will be run after deploying the app"},"main": "./lib/http-server","repository": {
"type": "git","url": "https://github.com/nodejitsu/http-server.git"},"keywords": [
"cli",
"http",
"server"
],"dependencies" : {
"colors" : "*","flatiron" : "0.1.x","optimist" : "0.2.x","union" : "0.1.x","ecstatic" : "0.1.x","plates" : "https://github.com/flatiron/plates/tarball/master"},"analyze": false,"devDependencies": {
"vows" : "0.5.x","request" : "2.1.x"},"bundleDependencies": [
"union",
"ecstatic"
],"license": "MIT","engines": {
"node": ">=0.6"}}
参数解释(不翻译咯):
1)name:
The unique name of your package.
This will also indicate the name of the package in the NPM global repository ( if you choose to publish it. )
On Nodejitsu, this property will represent the name of your application.
2)preferGlobal:
Flag that indicates this package prefers to be installed globally.
This is usually reserved for packages that contain command line interfaces ( CLIs ).
In most situations, you will NOT use this property.
3)version:(稍微重要点)
Version of the package as specified bySemantic Versioning.
It's important to keep track of your package versions in a smart way. If you don't follow standard versioning techniques, it will be difficult for users to keep track of your packages.
Consider the following version updates:
0.1.0 -> 0.1.1 should be non-breaking.
0.1.1 -> 0.2.0 could be breaking.
4)author:
The author of the project.
Hopefully one day soon, it will be your name!
5)description:
The description of the project.
Try to keep it short and concise.
6)contributors:
An array of objects representing contributors to the project.
Each object represents one contributor.
7)bin:
A hash containing key/pair mappings of binary script names and node.js script paths.
This is used to expose binary scripts from your package. It's useful for creating command line interfaces.
8)scripts:
A hash containing key/pair mappings of npm commands and node.js script paths.
This is used to map specific entry points into your package that npm can use in all sorts of cool ways.
9)main:
The main entry point of the package.
When calling require('http-server') in node.js this is the file that will actually be required.
It's highly advised that requiring the main fileNOT generate any side-effects.
For instance, requiring the main file shouldNOT start up an HTTP server or connect to database. Instead, you should create something like exports.init in your mainscript.
10)repository:
A hash containing key/pair mappings of source code repositories.
In our case, we will specify a git repository hosted on Github
11)key words:
An array of keywords which describe your package.
This is useful for users who search for packages on search.npmjs.org
12)dependency:
A hash containing key/pair mappings of npm packages and versions.
This is used to specify the dependencies for your packages.
13)bundleDependency:
An array containing a list of package names you have bundled in your package.
The convention here is to make sure your bundled dependencies exist in thenode_modules/ folder.
Packages listed in bundleDependencies will now remain locked into the version contained in the node_modules/ folder.
14)license :
The license which you prefer to release your project under.
MIT is a good choice.
15)engine:
A hash containing key/pair mappings ofengine versions.
This is used to specify the versions of node.jsyour package is known to work correctly with.
或者命令行下执行npm init -- 命令行按提示生成package.json
第三步:
一个简单的js文件app.js
var os = require('os');
exports.HiOS = function () {
console.log(os.type());}
第四步:
生成npm入口执行文件init.js(脚本配置)
#!/usr/bin/env nodevar osType = require('../app.js').HiOS;
osType();
第五步:
添加npm本地用户 :npm addUSer 按提示操作
第六步:
发布 当前文件目录 npm publish
第七步:
本地安装测试 npm install jiangc –g
OK
参考(Reference)
1.https://github.com/joyent/node/wiki/Resources
2.package.json 详解http://package.json.nodejitsu.com/