使用 concurrently 并行地运行多个命令

项目目录结构如下,想要实现一个命令同时跑前端和后端,可以使用 concurrently

1
2
3
4
5
6
7
8
.
├── client // 前端代码
├── db // 后端代码
├── logs // 日志
├── node_modules
├── package-lock.json
├── package.json
└── README.md
  1. 在项目根目录安装

    1
    npm i concurrently --save
  2. 修改 package.json

    1
    2
    3
    4
    5
    6
    "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "client": "npm start --prefix client",
    "server": "nodemon ./db/index.ts",
    "dev": "concurrently \"npm run server\" \"npm run client\""
    }
  3. 执行

    1
    2
    3
    npm run client # 执行前端代码
    npm run server # 执行后端代码
    npm run dev # 同时执行前面两条代码