在 package.json 中定义的一组内置脚本和自定义脚本,提供一种简单的方法来执行重复的任务。例如:启动项目,打包项目,执行单元测试。
注意,npm 中所有依赖的 node_modules bin 都可以在脚本中直接访问
下面所有命令效果都是一样的
$ npm run-script start
$ npm run start
$ npm start
还可以执行多个脚本
# 依次运行多个脚本
$ npm run lint && npm test
# 并行运行多个脚本,只适用于 Unix 环境,Windows 中将按顺序执行
$ npm run lint & npm test
完整示例如下
{
"script": {
"lint": "eslint .", // 与 "./node_modules/.bin/eslint ." 写法效果相同
"test": "echo \"Error: no test specified\" && exit 1",
"ci": "npm run lint && npm test"
}
}