npm run
执行 package.json 中定义的脚本命令
语法
npm run <script> [-- <args>]
参数
| 参数 | 说明 | 示例 | 级别 |
|---|---|---|---|
<script> |
执行指定脚本 | npm run build |
常用 |
-- <args> |
传递参数给脚本 | npm run test -- --watch |
常用 |
(无参数) |
列出所有可用脚本 | npm run |
常用 |
start |
npm start 是 npm run start 的简写 | npm start |
常用 |
test |
npm test 是 npm run test 的简写 | npm test |
常用 |
示例
运行构建脚本
npm run build
执行 package.json 中 scripts.build 定义的命令
运行开发服务器
npm run dev
传递参数
npm run test -- --coverage
-- 后面的参数传给实际脚本
查看所有脚本
npm run
列出 package.json 中所有 scripts
常见错误
Missing script: "xxx"
package.json 的 scripts 中没有该脚本,用 npm run 查看可用脚本
sh: command not found
脚本中使用的命令未安装,检查 devDependencies 或全局安装
技巧
- npm start、npm test、npm stop 不需要 run 前缀
- pre/post 钩子:pretest 会在 test 之前自动执行
- 用 npx 可以直接运行 node_modules/.bin 中的命令