什么是 npm 脚本,npm 的生命周期脚本是如何工作的?
npm scripts 是在 package.json 的 scripts 字段中定义的命令,用于自动化项目的常见任务。基本语法{ "scripts": { "start": "node index.js", "dev": "nodemon index.js", "build": "webpack --mode production", "test": "jest", "lint": "eslint src/" }}运行脚本使用 npm run <script-name> 或 npm <script-name>(对于 start、stop、test 等特殊命令):npm run devnpm startnpm test生命周期脚本npm 提供了特殊的生命周期脚本,在特定事件时自动执行:安装相关生命周期{ "scripts": { "preinstall": "echo 'Installing dependencies...'", "install": "node setup.js", "postinstall": "echo 'Dependencies installed!'", "preuninstall": "echo 'Uninstalling...'", "postuninstall": "echo 'Uninstalled!'" }}执行顺序:preinstallinstallpostinstall发布相关生命周期{ "scripts": { "prepublishOnly": "npm run build && npm run test", "prepack": "npm run build", "postpack": "echo 'Package packed!'", "publish": "node publish.js", "postpublish": "echo 'Published!'" }}执行顺序:prepublishOnlyprepackpreparepostpackpublishpostpublish其他生命周期{ "scripts": { "prestart": "npm run build", "start": "node index.js", "poststart": "echo 'Server started!'", "pretest": "npm run lint", "test": "jest", "posttest": "npm run coverage" }}脚本参数传递可以向脚本传递参数:{ "scripts": { "test": "jest", "test:watch": "jest --watch", "test:coverage": "jest --coverage" }}运行时传递额外参数:npm run test -- --verbose-- 后面的参数会传递给脚本命令。环境变量npm scripts 可以访问 npm 提供的环境变量:{ "scripts": { "build": "webpack --env.NODE_ENV=$npm_package_config_env", "start": "node $npm_package_main" }}常用环境变量:npm_package_name:包名npm_package_version:包版本npm_package_main:主入口文件npm_package_scripts_*:其他脚本npm_config_*:npm 配置npm_lifecycle_event:当前生命周期事件名跨平台兼容性npm scripts 在不同操作系统上的兼容性处理:使用 cross-env{ "scripts": { "build": "cross-env NODE_ENV=production webpack" }}使用 rimraf(跨平台删除){ "scripts": { "clean": "rimraf dist" }}常见脚本模式开发环境脚本{ "scripts": { "dev": "nodemon index.js", "dev:debug": "nodemon --inspect index.js", "dev:hot": "webpack serve --hot" }}构建脚本{ "scripts": { "build": "webpack --mode production", "build:dev": "webpack --mode development", "build:analyze": "webpack --mode production --analyze" }}测试脚本{ "scripts": { "test": "jest", "test:watch": "jest --watch", "test:coverage": "jest --coverage", "test:ci": "jest --ci --coverage --maxWorkers=2" }}代码质量脚本{ "scripts": { "lint": "eslint src/", "lint:fix": "eslint src/ --fix", "format": "prettier --write \"src/**/*.js\"", "typecheck": "tsc --noEmit" }}Git 钩子脚本(使用 husky){ "scripts": { "prepare": "husky install", "precommit": "lint-staged" }}并行和顺序执行使用 & 并行执行,使用 && 顺序执行:{ "scripts": { "parallel": "npm run lint & npm run test", "sequential": "npm run lint && npm run test", "all": "npm run lint && npm run test && npm run build" }}使用 npm-run-all 工具更灵活:{ "scripts": { "dev": "npm-run-all --parallel dev:*", "dev:server": "nodemon index.js", "dev:client": "webpack serve" }}最佳实践命名规范:使用冒号分隔相关脚本(如 test:watch)文档化:在 README 中说明自定义脚本的作用错误处理:确保脚本在失败时返回非零退出码性能优化:避免不必要的重复操作环境隔离:使用环境变量管理不同环境配置依赖管理:将开发工具作为 devDependencies调试技巧查看脚本执行详情npm run <script> -- --verbose查看实际执行的命令npm run <script> -n使用 npm link 调试本地包npm link ../my-local-packagenpm scripts 是项目自动化的核心,合理使用可以显著提高开发效率和项目可维护性。