1. 程式人生 > >開發發布npm module包

開發發布npm module包

ini 小時 工具 public make 功能 上一個 再次 分享

開發發布npm module包

問題

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

前期準備工作

  • 安裝nodejs
  • github上新建一個repository用於托管組件代碼
  • 新建一個npm賬戶用於發布包

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

1,創建組件

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

mkdir timeFormat

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

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

技術分享圖片
技術分享圖片

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

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

具體代碼內容:

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

技術分享圖片

表示成功

2,發布包至npm

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

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

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

技術分享圖片

查看當前用戶:

$npm whoami

顯示當前用戶名
發布包

$ npm publish

技術分享圖片

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

再次發布,成功後我們去npm官網就能搜到該組件了
技術分享圖片
技術分享圖片

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

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

技術分享圖片

執行測試文件

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

console.log(time);

成功。

開發發布npm module包