開發發佈npm module包

開發發佈npm module包

問題

在項目開發過程當中,每當進入一個新的業務項目,從零開始搭建一套前端項目結構是一件讓人頭疼的事情,就要從新複製一個上一個項目的前端框架和組件代碼庫。其中不少功能的模塊組件都要重複拷貝,能夠統一將這些組件類的模塊統一打包上傳至npm,之後每次都只須要install一下就能夠了。前端

前期準備工做

  • 安裝nodejs
  • github上新建一個repository用於託管組件代碼
  • 新建一個npm帳戶用於發佈包

這裏以工具組件庫中的時間格式轉換工具爲例,主要用於對Date時間進行不一樣格式轉換。node

1,建立組件

新建一個用於放置時間格式轉換npm包的文件夾git

mkdir timeFormat

初始化配置包json文件package.jsongithub

npm init

$ 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: (node) timeForamt
version: (0.0.0) 0.0.1
description: An easy mongodb client for node.js based on native mongodb driver.
entry point: (index.js) 
test command: make test
git repository: https://github.com/summer7310/timeformat.git
keywords: timeformat 
author: summer7310 
license: (BSD-2-Clause) MIT

clipboard.png

clipboard.png

package.json文件主要用來表述項目信息的,包括名稱,版本,依賴,版權,git地址。mongodb

在文件夾下新建時間格式轉化功能腳本入口文件index.jsnpm

具體代碼內容:json

index.js

// 月(M)、日(d)、小時(h)、分(m)、秒(s)、季度(q) 能夠用 1-2 個佔位符, 
// 年(y)能夠用 1-4 個佔位符,毫秒(S)只能用 1 個佔位符(是 1-3 位的數字) 
function timeFormat(date, fmt) {

    var o = {
        "M+": date.getMonth() + 1, //月份 
        "d+": date.getDate(), //日 
        "h+": date.getHours(), //小時 
        "m+": date.getMinutes(), //分 
        "s+": date.getSeconds(), //秒 
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度 
        "S": date.getMilliseconds() //毫秒 
    };
    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

exports.timeFormat = timeFormat;

每一個組件包須要配一個測試文件用於測試,新建test.js前端框架

test.js

var fn = require('./index.js');
var time = fn.timeFormat(new Date(), "yyyy-MM-dd");
console.log(time);

執行test網絡

node test.js

輸出:框架

D:\npm_test\test>node test.js
2017-12-04

clipboard.png

表示成功

2,發佈包至npm

在npm官網註冊帳號,https://www.npmjs.com

添加用戶,輸入完用戶名,密碼,郵箱後沒有錯誤信息就完成了。

$ npm adduser
Username: your name
Password: your password
Email: (this IS public) your email

clipboard.png

查看當前用戶:

$npm whoami

顯示當前用戶名
發佈包

$ npm publish

clipboard.png

發現報錯,用戶沒有權限發佈該包,這時候去npm官網查一下,發現已經存在該名字的npm包,解決方法就是重命名咱們的組件名字,更名爲timeFormatUtil

clipboard.png

再次發佈,成功後咱們去npm官網就能搜到該組件了

clipboard.png

這裏大概再羅列一下發布過程當中可能遇到的問題和解決方法。

Q1:

npm ERR! no_perms Private mode enable, only admin can publish this module:

這裏注意的是由於國內網絡問題,許多小夥伴把npm的鏡像代理到淘寶或者別的地方了,這裏要設置回原來的鏡像。

npm config set registry=http://registry.npmjs.org

Q2:

npm ERR! you do not have permission to publish "your module name". Are you logged in as the correct user?

提示沒有權限,其實就是你的module名在npm上已經被佔用啦,這時候你就去須要去npm搜索你的模塊名稱,若是搜索不到,就能夠用,而且把package.json裏的name修改過來,從新npm publish。

注意

每次修改組件須要從新發布都必須修改當前版本號,要否則npm會認爲衝突。

3,下載使用組件

在項目中執行

npm install timeformatutil --save

clipboard.png

執行測試文件

const fn = require('timeformatutil');
let time = fn.timeFormat(new Date(), "yyyy-MM-dd");

console.log(time);

成功。

相關文章
相關標籤/搜索