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