Node.js自定义包的创建和发布

先创建文件夹,包含这几个文件:

 

 

 

 

再package.json里面写入:

{
  "name": "itheima-tools",
  "version": "1.0.0",   
  "main": "index.js",  
  "description": "提供格式化时间,HTMLEscape相关功能   main--包的入口文件  keywords--搜索关键字 license--开源许可协议",
  "keywords": [  
    "itheima",
    "dataFormat",
    "escape"
  ],
  "license": "ISC",
}

index.js:

//这是包的入口文件
const date = require('./src/dateFormat')
module.exports ={
  ...date
}

dataFormat.js:

//定义格式化时间的函数
function dateFormat(dateStr){
  const dt = new Date(dateStr)

  const y = dt.getFullYear()
  const m = padZero(dt.getMonth()+1)
  const d = padZero(dt.getDate())

  const hh = padZero(dt.getHours())
  const mm = padZero(dt.getMinutes())
  const ss = padZero(dt.getSeconds())

  return `${y}-${m}-${d} ${hh}:${mm}:${ss}`
}

// 定义补零的方法
function padZero(n){
  return n > 9 ? n : '0'+n
}

module.exports = {
  dateFormat
}

 

 

切换npm服务器连接:https://www.cnblogs.com/gaozejie/p/10694834.html 

 

 

 

 

 

 

 

 

  

posted on 2022-03-20 16:38  IT丶Hatcher  阅读(166)  评论(0)    收藏  举报

导航