npm 的内置生命周期脚本(Lifecycle Scripts)是一系列预定义的钩子,会在执行特定 npm 命令时自动触发 ,核心分为「核心命令钩子」「发布相关钩子」「安装/卸载钩子」等类别,且遵循 prexxx → xxx → postxxx 的执行顺序(如 prebuild → build → postbuild)。
以下是完整的内置生命周期脚本分类及说明:
一、核心命令钩子(最常用)
这类钩子对应 npm run <cmd> 或 npm <cmd> 核心命令,是日常开发中最常使用的:
| 钩子名 | 触发时机 | 示例场景 |
|---|---|---|
pre<cmd> |
执行 npm run <cmd> 前自动触发 |
prebuild → 执行 npm run build 前 |
post<cmd> |
执行 npm run <cmd> 后自动触发 |
postbuild → 执行 npm run build 后 |
pretest |
执行 npm test 前触发 |
测试前清理测试目录、校验环境 |
test |
执行 npm test 时触发(需手动配置脚本) |
运行单元测试(如 jest、mocha) |
posttest |
执行 npm test 后触发 |
测试后生成测试报告、清理临时文件 |
prestart |
执行 npm start 前触发 |
启动前检查端口、初始化配置 |
start |
执行 npm start 时触发(需手动配置) |
启动项目(如 node server.js) |
poststart |
执行 npm start 后触发 |
启动后输出日志、发送健康检查 |
prestop |
执行 npm stop 前触发 |
停止前保存进程状态 |
stop |
执行 npm stop 时触发(需手动配置) |
停止项目(如 kill 进程) |
poststop |
执行 npm stop 后触发 |
停止后清理资源 |
prerestart |
执行 npm restart 前触发(= prestop + prestart) |
重启前预处理 |
restart |
执行 npm restart 时触发(= stop + start) |
重启项目 |
postrestart |
执行 npm restart 后触发(= poststop + poststart) |
重启后校验 |
二、包发布相关钩子(npm publish)
这类钩子仅在发布 npm 包时触发,是发布流程的关键节点:
| 钩子名 | 触发时机 |
|---|---|
prepublish |
(已废弃,不推荐) 执行 npm publish 或 npm pack 前触发 |
prepare |
替代 prepublish,执行 npm publish/npm pack/npm install 时触发 |
prepublishOnly |
仅在 npm publish 前触发(不会在 npm install 时触发,推荐替代 prepublish) |
prepack |
执行 npm pack/npm publish 前触发(打包 tarball 前) |
postpack |
执行 npm pack/npm publish 后触发(打包 tarball 后) |
publish/postpublish |
执行 npm publish 成功后触发(发布到 npm 仓库后) |
关键区别:
prepare:npm install/publish/pack都会触发(适合编译源码、生成产物);prepublishOnly:仅npm publish触发(适合发布前校验、签名等)。
三、安装/卸载相关钩子(npm install/uninstall)
这类钩子在安装/卸载包(本地/全局)时触发,注意:仅对当前包生效,不影响依赖包:
| 钩子名 | 触发时机 |
|---|---|
preinstall |
执行 npm install 前触发(当前包安装前) |
install/postinstall |
执行 npm install 成功后触发(当前包安装后,适合编译二进制、初始化配置) |
preuninstall |
执行 npm uninstall 前触发(当前包卸载前) |
uninstall/postuninstall |
执行 npm uninstall 后触发(当前包卸载后,适合清理残留文件) |
preversion |
执行 npm version <newver> 前触发(修改版本号前,如校验版本格式) |
version |
执行 npm version <newver> 时触发(修改版本号后,如更新 CHANGELOG) |
postversion |
执行 npm version <newver> 后触发(修改版本号后,如提交 git 版本) |
四、其他特殊钩子
| 钩子名 | 触发时机 |
|---|---|
audit |
执行 npm audit 时触发(需手动配置,用于自定义审计逻辑) |
preaudit/postaudit |
执行 npm audit 前/后触发 |
fund |
执行 npm fund 时触发(自定义开源资助信息展示) |
ci |
执行 npm ci 时触发(替代 npm install,适合 CI/CD 环境) |
preci/postci |
执行 npm ci 前/后触发 |
五、核心规则(必知)
- 执行顺序 :
prexxx失败 →xxx和postxxx不会执行;prexxx成功 → 执行xxx→ 无论xxx成功/失败,都会执行postxxx(除非进程终止)。 - 自定义脚本兼容 :对自定义脚本(如
npm run build),自动生成prebuild/postbuild钩子,无需额外配置。 - 跨平台兼容 :Windows/macOS/Linux 通用,推荐使用
rimraf/mkdirp替代系统命令(如rm -rf/mkdir -p)。 - 优先级 :若手动配置了钩子脚本(如
package.json中写了"prebuild": "xxx"),则执行自定义逻辑;无配置则不执行。
六、实用示例(结合你的场景)
json
{
"scripts": {
// 打包前清理旧产物
"prebuild": "rimraf static-customer",
// 打包 + 压缩
"build": "father build && node scripts/zip-dist.js",
// 打包后提示
"postbuild": "echo '✅ 打包+压缩完成!'",
// 发布前校验版本和产物
"prepublishOnly": "npm run build && node scripts/check-publish.js",
// 安装后初始化配置
"postinstall": "node scripts/init-config.js"
}
}
七、如何查看当前包的生命周期脚本
执行以下命令可列出当前项目已配置的所有生命周期脚本:
bash
npm run ls
或查看完整的生命周期执行顺序:
bash
npm help scripts
总结:日常开发中最常用的是 prexxx/postxxx(如 prebuild/postbuild)、发布相关的 prepare/prepublishOnly、安装相关的 postinstall,掌握这些即可覆盖 90% 的场景。