Appearance
concurrently
简介
concurrently 是一个用于同时运行多个命令的 Node.js 工具,它可以在同一个终端窗口中并行执行多个命令,非常适合需要同时启动多个服务的开发环境。
安装
bash
npm install --save-dev concurrently基本使用
1. 在 package.json 中使用
json
{
"scripts": {
"start": "concurrently \"npm run server\" \"npm run client\"",
"server": "nodemon server.js",
"client": "react-scripts start"
}
}2. 命令行直接使用
bash
concurrently "npm run watch-js" "npm run watch-css" "npm run dev-server"高级特性
1. 自定义命令名称
bash
concurrently -n "server,client" "npm run server" "npm run client"2. 设置颜色
bash
concurrently --kill-others -n "server,client" -c "red,green" "npm run server" "npm run client"3. 配置选项
javascript
concurrently[
({ command: 'npm run server', name: 'server', prefixColor: 'blue' },
{ command: 'npm run client', name: 'client', prefixColor: 'green' })
]最佳实践
错误处理
json{ "scripts": { "start": "concurrently --kill-others-on-fail \"npm run server\" \"npm run client\"" } }等待所有命令完成
bashconcurrently --success first "npm run test" "npm run lint"重启失败的命令
bashconcurrently --restart-tries 3 "npm run watch-js" "npm run watch-css"
常用选项
--kill-others: 当一个命令退出时,终止其他所有命令--kill-others-on-fail: 当一个命令失败时,终止其他所有命令--restart-tries: 设置重启尝试次数--prefix: 设置输出前缀(进程名或索引)--prefix-colors: 设置前缀颜色--timings: 显示每个命令的启动时间
注意事项
- 在 Windows 环境下使用时注意命令的转义
- 避免在生产环境中使用
- 注意内存使用,不要同时运行太多命令
- 合理使用 kill-others 选项
- 注意命令的依赖关系