抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

创建包

1
2
3
npm init
npm i -D typescript
npx tsc --init

package.json

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
{
"name": "owle",//包名
"version": "1.0.0",//版本号
"type": "module",//类型,加了就是es模块
"main": "./dist/cjs/index.js",//主入口(CommonJS,供 require())
"module": "./dist/esm/index.js",//ES 模块入口(供 import)
"types": "./dist/esm/index.d.ts",//TypeScript 类型定义文件位置
"exports": {//输出文件
".": {
"types": "./dist/esm/index.d.ts",
"import": "./dist/esm/index.js",
"require": "./dist/cjs/index.js"
}
},
"files": [//输出目录
"dist"
],
"repository": {//git配置
"type": "git",
"url": ""
},
"scripts": {//脚本命令
"build": "npm run build:esm && npm run build:cjs",
"build:esm": "tsc -p tsconfig.esm.json",
"build:cjs": "tsc -p tsconfig.cjs.json",
"clean": "rm -rf dist"
},
"author": "zhixin huang",//作者
"license": "ISC",//许可证
"description": "owle is a library for building web applications",//项目描述
"devDependencies": {//开发依赖
"typescript": "^5.9.3"
}
}

.github/workflows/npm-publish.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69

# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages

name: Node.js Package

# 触发条件
on:
# 推送标签时触发
push:
tags:
- '*' # 匹配所有的标签,如 1.0.0, 1.0.1
# 创建 GitHub Release 时触发
release:
types: [created]
# 手动触发工作流
workflow_dispatch:

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: 检出代码
uses: actions/checkout@v4

- name: 设置 Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'

- name: 安装依赖
run: npm ci

- name: 构建项目
run: npm run build

- name: 运行测试
run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
# 推送标签、创建 release 或手动触发时执行发布
if: github.event_name == 'release' || github.event_name == 'push' || github.event_name == 'workflow_dispatch'

steps:
- name: 检出代码
uses: actions/checkout@v4

- name: 设置 Node.js
uses: actions/setup-node@v4
with:
node-version: 20
registry-url: https://registry.npmjs.org/
cache: 'npm'

- name: 安装依赖
run: npm ci

- name: 构建项目
run: npm run build

- name: 发布到 npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

.github/workflows/deploy-docs.yml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
name: Deploy Docs to GitHub Pages

on:
push:
branches:
- main
paths:
- 'doc/**'
- '.github/workflows/deploy-docs.yml'
workflow_dispatch:

permissions:
contents: read
pages: write
id-token: write

concurrency:
group: "pages"
cancel-in-progress: false

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: 检出代码
uses: actions/checkout@v4

- name: 设置 Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: 'npm'
cache-dependency-path: doc/package-lock.json

- name: 安装依赖
working-directory: ./doc
run: npm ci

- name: 构建文档站点
working-directory: ./doc
run: npm run build

- name: 设置 Pages
uses: actions/configure-pages@v4

- name: 上传 artifact
uses: actions/upload-pages-artifact@v3
with:
path: doc/dist

deploy:
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
runs-on: ubuntu-latest
needs: build
steps:
- name: 部署到 GitHub Pages
id: deployment
uses: actions/deploy-pages@v4


发布

1
2
npm version patch
git push

npm常用命令

1. 包管理命令

安装依赖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 安装所有依赖
npm install
npm i

# 安装生产依赖
npm install package-name
npm i package-name

# 安装开发依赖
npm install --save-dev package-name
npm i -D package-name

# 安装全局包
npm install -g package-name

# 安装特定版本
npm install package-name@1.2.3
npm install package-name@latest

卸载依赖

1
2
3
4
5
6
7
8
9
10
# 卸载包
npm uninstall package-name
npm un package-name

# 卸载开发依赖
npm uninstall --save-dev package-name
npm uninstall -D package-name

# 卸载全局包
npm uninstall -g package-name

更新依赖

1
2
3
4
5
6
7
8
9
10
11
# 更新所有依赖
npm update

# 更新特定包
npm update package-name

# 更新全局包
npm update -g package-name

# 检查过时的包
npm outdated

2. 项目初始化命令

1
2
3
4
5
6
7
8
# 创建 package.json(交互式)
npm init

# 快速创建(使用默认值)
npm init -y

# 创建范围包
npm init --scope=username

3. 脚本执行命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 运行脚本
npm run script-name

# 常用内置脚本
npm start
npm test
npm stop
npm restart

# 运行前置和后置脚本
npm pretest # 在 test 之前运行
npm posttest # 在 test 之后运行

# 查看所有可用脚本
npm run

4. 包发布命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 登录 npm
npm login

# 退出登录
npm logout

# 查看当前登录用户
npm whoami

# 发布包
npm publish

# 撤销发布(24小时内)
npm unpublish package-name@version

# 弃用包版本
npm deprecate package-name@version "message"

5. 信息查看命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 查看包信息
npm info package-name
npm view package-name

# 查看包文档
npm docs package-name

# 查看包首页
npm home package-name

# 查看包仓库
npm repo package-name

# 搜索包
npm search keyword
npm find keyword

# 列出已安装包
npm list
npm ls

# 列出全局包
npm list -g --depth=0

6. 配置管理命令

1
2
3
4
5
6
7
8
9
10
11
12
# 查看配置
npm config list
npm config get registry

# 设置配置
npm config set registry https://registry.npmjs.org/

# 删除配置
npm config delete key-name

# 编辑配置文件
npm config edit

7. 缓存管理命令

1
2
3
4
5
6
7
8
# 查看缓存信息
npm cache verify

# 清理缓存
npm cache clean --force

# 查看缓存位置
npm config get cache

8. 版本管理命令

1
2
3
4
5
6
7
8
9
10
# 版本号递增
npm version patch # 修订号 1.0.0 → 1.0.1
npm version minor # 次版本号 1.0.0 → 1.1.0
npm version major # 主版本号 1.0.0 → 2.0.0

# 预发布版本
npm version prerelease --preid=beta

# 指定版本号
npm version 2.1.0

9. 审计和安全命令

1
2
3
4
5
6
7
8
9
10
11
# 检查安全漏洞
npm audit

# 自动修复漏洞
npm audit fix

# 强制修复(可能破坏兼容性)
npm audit fix --force

# 生成安全报告
npm audit --json

10. 工作区命令(Monorepo)

1
2
3
4
5
6
7
8
# 在工作区中安装
npm install package-name -w packages/workspace-name

# 在工作区中运行脚本
npm run test -w packages/workspace-name

# 列出所有工作区
npm workspaces list

11. 其他实用命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 检查包是否已安装
npm list package-name

# 查看包目录
npm root
npm root -g

# 解释包解析过程
npm explain package-name

# 检查包依赖关系
npm ls --all

# 生成锁文件
npm shrinkwrap

12. 调试命令

1
2
3
4
5
6
7
8
9
# 详细日志
npm install --verbose
npm install --dd # 调试模式

# 只生成锁文件,不安装
npm install --package-lock-only

# 忽略脚本执行
npm install --ignore-scripts

13. 常用组合命令示例

开发工作流

1
2
3
4
5
6
7
# 初始化新项目
mkdir my-project && cd my-project
npm init -y
npm install express
npm install -D nodemon eslint
npm set-script start "node index.js"
npm set-script dev "nodemon index.js"

发布工作流

1
2
3
4
5
# 准备发布
npm version patch
npm run test
npm run build
npm publish

依赖管理

1
2
3
4
5
6
# 更新所有依赖到最新
npx npm-check-updates -u
npm install

# 或者使用内置命令
npm update --save

14. 实用技巧

查看脚本内容

1
2
# 查看实际执行的脚本命令
npm run script-name --dry-run

并行运行脚本

1
2
3
# 使用 npm-run-all 包
npm install -D npm-run-all
npm-run-all --parallel task1 task2

环境变量设置

1
2
# 设置环境变量运行
NODE_ENV=production npm run build

15. 常用配置示例

package.json 常用脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"scripts": {
"start": "node index.js",
"dev": "nodemon index.js",
"test": "jest",
"test:watch": "jest --watch",
"build": "webpack --mode=production",
"build:dev": "webpack --mode=development",
"lint": "eslint src/",
"lint:fix": "eslint src/ --fix",
"type-check": "tsc --noEmit",
"clean": "rm -rf dist/",
"prepublishOnly": "npm test && npm run build"
}
}

评论