这个代码库用户存放npm插件的代码。javascript
mkdir npmjs
cd npmjs
npm init
ubuntuvimdeMacBook-Pro:randomNichname ubuntuvim$ npm init This utility will walk you through creating a package.json file. It only covers the most common items, and tries to guess sensible 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: (randomNichname) Sorry, name can no longer contain capital letters. name: (randomNichname) randomNichname Sorry, name can no longer contain capital letters. name: (randomNichname) y version: (1.0.0) 0.1.0 description: Get the name of the three kingdoms. entry point: (index.js) y test command: git repository: https://github.com/ubuntuvim/randomNichname.git keywords: nickname,random author: ubuntuvim license: (ISC) ISC About to write to /Users/ubuntuvim/codes/my-npm-plugins/randomNichname/package.json: { "name": "y", "version": "0.1.0", "description": "Get the name of the three kingdoms.", "main": "y", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "repository": { "type": "git", "url": "git+https://github.com/ubuntuvim/randomNichname.git" }, "keywords": [ "nickname", "random" ], "author": "ubuntuvim", "license": "ISC", "bugs": { "url": "https://github.com/ubuntuvim/randomNichname/issues" }, "homepage": "https://github.com/ubuntuvim/randomNichname#readme" } Is this ok? (yes) yes ubuntuvimdeMacBook-Pro:randomNichname ubuntuvim$
建立完成package.json以后你也能够根据实际状况作修改。html
npmjs ├─┬ lib │ └── npmjs.js ├─┬ test │ └── test.js ├── .gitignore ├── .npmignore ├── .travis.yml ├── index.js ├── LICENSE ├── makefile ├── package.json ├── README.md
这些文件的做用是:java
主要代码直接放在index.js
。好比本插件是用于获取随机的三国人物名称,代码以下(详细代码请从github下载):node
// 从1099个名字中获取任意名字 (function () { 'use strict'; // 一共1099个 var arr = []; // 获取随机名字 function getNickname() { // 取0~1099的随机数 var random = Math.floor(Math.random() * 1099); if (random >= 1099) throw new Error("获取人名数组下标月结!"); return arr[random]; } exports.getNickname = getNickname; }());
var arr = require('../index'); var s = arr.getNickname(); if (s) { console.log(s); } else { throw new Error('The test does not pass...'); }
运行测试。 node ./test/test.js
git
许可内容请从github下载。github
插件使用方式shell
安装插件 npm install randomNickname
npm
使用插件json
var names = require('randomNickname'); var s = names.getNickname(); console.log('获得的人物名字为: ' + s);
步骤以下:ubuntu
git init
git remote add origin <git远程URL>
git add *
git commit -am '描述信息'
git push origin master
若是出现相似以下错误! [rejected] master -> master (non-fast-forward) error: failed to push some refs to 'https://github.com/ubuntuvim/randomNickname.git' hint: Updates were rejected because the tip of your current branch is behind hint: its remote counterpart. Integrate the remote changes (e.g. hint: 'git pull ...') before pushing again. hint: See the 'Note about fast-forwards' in 'git push --help' for details.
先更新合并再提交到远程代码库。
git pull
git merge origin/master
git push origin/master
步骤以下:
npm adduser
(输入你在npmjs官网注册的帐号和密码)npm publish .
若是出现错误: no_perms Private mode enable, only admin can publish this module
执行下列代码重置后再执行npm adduser
、npm publish .
。
npm config set registry http://registry.npmjs.org
等待完成,看到以下信息说明发布成功。
+ randomnickname@0.1.0
若是你的代码修改了又须要从新发布到npmjs上,则首先修改package.json
里的版本号,再执行npm publish .
便可。
此时你能够到npmjs的我的中心中查看刚刚发布的插件。 好比个人我的中心https://www.npmjs.com/~ubuntuvim。
参考文献: