在package.json中,我有两个脚本:

  "scripts": {
    "start-watch": "nodemon run-babel index.js",
    "wp-server": "webpack-dev-server",
  }

每次开始在Node.js中开发时,我都必须并行运行这两个脚本。我首先想到的是添加第三个脚本,如下所示:

"dev": "npm run start-watch && npm run wp-server"

…但这将在运行wp服务器之前等待开始监视完成。

如何并行运行这些?请记住,我需要查看这些命令的输出。此外,如果您的解决方案涉及构建工具,我宁愿使用gulf而不是gulf,因为我已经在另一个项目中使用了它。


当前回答

在父文件夹的package.json中:

"dev": "(cd api && start npm run start) & (cd ../client && start npm run start)"

这在windows中工作

其他回答

我有一个没有任何额外模块的跨平台解决方案。我正在寻找一个可以在cmd.exe和bash中使用的try-catch块。

解决方案是command1||command2,它似乎在两种环境中都一样。因此,OP的解决方案是:

"scripts": {
  "start-watch": "nodemon run-babel index.js",
  "wp-server": "webpack-dev-server",
  // first command is for the cmd.exe, second one is for the bash
  "dev": "(start npm run start-watch && start npm run wp-server) || (npm run start-watch & npm run wp-server)",
  "start": "npm run dev"
}

然后,简单的npm-start(和npm-run-dev)将在所有平台上运行!

如果将双&号替换为单&号,脚本将同时运行。

使用concurrent可与共享输出流并行运行命令。为了便于区分哪个输出来自哪个进程,请使用缩短的命令格式,例如npm:wp-server。这将导致同时在每个输出行前面加上命令名。

在package.json中,脚本部分如下所示:

 "scripts": {
    "start": "concurrently \"npm:start-watch\" \"npm:wp-server\"",
    "start-watch": "nodemon run-babel index.js",
    "wp-server": "webpack-dev-server"
  }

我已经使用npm-run all有一段时间了,但我一直都不习惯,因为在监视模式下命令的输出不能很好地一起工作。例如,如果我在监视模式下开始创建react应用程序和jest,我将只能看到上次运行的命令的输出。所以大多数时候,我都在手动运行所有命令。。。

这就是为什么,我实现了自己的lib,运行屏幕。它仍然是一个非常年轻的项目(从昨天开始:p),但它可能值得一看,在您的情况下,它将是:

run-screen "npm run start-watch" "npm run wp-server"

然后按数字键1查看wp服务器的输出,按0查看start watch的输出。

npm-run-all --parallel task1 task2

编辑:

您需要事先安装npm runall。还请检查此页面以了解其他使用情况。