发布NPM包

4/10/2023 Node

# 包目录结构:必须包含index.js,package.json,README.md三个文件

在这里插入图片描述

# index.js 这里写自己要是显得功能然后module.exports暴露出去

// 这是包的入口文件
const date = require('./src/dateFormat')
const escape = require('./src/htmlEscape')


module.exports = {
    ...date,
    ...escape
}
1
2
3
4
5
6
7
8
9

# package.json 这是包的信息

{
  "name": "luffy-tools",
  "version": "1.0.0",
  "description": "提供了格式化时间,HTMLEscape的功能",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": ["luffy","dateFormat","escape"],
  "author": "luffy",
  "license": "ISC"
}

1
2
3
4
5
6
7
8
9
10
11
12
13

# README.md里面写描述:安装,导入,和使用

# 安装

npm install luffy-tools
1

# 导入

const luffy = require('luffy-tools')
1

# 格式化时间

// 调用 dateFormat 对时间进行格式化
const dtStr = luffy.dateFormat(new Date())
// 结果 2020-04-03 17:20:58
console.log(dtStr);
1
2
3
4

# 发布

控制台操作:

npm login
//进行登录
cd luffy-tools
//cd 到要发布的包
npm publish
//发布
1
2
3
4
5
6

# 删除已发布的包

npm unpublish luffy-tools --force
1

NPM官网 (opens new window)

Last Updated: 11/8/2023, 10:40:28 AM