這個代碼庫用戶存放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。
參考文獻: