How can I run multiple npm scripts at the same time?

依然范特西╮ 提交于 2020-01-13 08:58:09

问题


In my package.json, I defined two scripts. How do I run them at the same time?

"scripts": {
    "server": "webpack-dev-server",
    "webpack": "webpack -wd",
},

回答1:


Invoke scripts via npm run with & for parallel execution or with && for sequential execution:

npm run server & npm run webpack

Explanation:

Use &&  for sequential execution.
Use &  for parallel execution.



回答2:


"scripts": {
    "sw": "webpack-dev-server & webpack -wd"
},

then

npm run sw



回答3:


You can use a module like parallelshel.

https://www.npmjs.com/package/parallelshell

As it says npm official site:

The biggest difference is that parallelshell is an npm module and GNU parallel isn't. While they probably do similar things, albeit (GNU) parallel being more advanced, parallelshell is an easier option to work with when using npm (because it's an npm module).

If you have GNU parallel installed on all the machines you project will be on, then by all means use it! :)

-

How is this different than:

$ cmd1 & cmd2 & cmd3

  • Cross platform -- works on Unix or Windows.



回答4:


Use concurrently to run multiple npm scripts.

Steps:

  1. Run npm i concurrently to install concurrently.

  2. Modified scripts in package.json by adding all (You may change to other name).

    "scripts": { "server": "webpack-dev-server", "webpack": "webpack -wd", "all": "concurrently \"npm run server\" \"npm run webpack\"" },

  3. Run npm run all to execute multiple npm scripts.

  4. Confirm the output in console log.



回答5:


You can use npm-run-all to combine multiple commands in a lot of different ways

For example, if you had the following scripts in your package.json:

"scripts": {
    "lint":  "eslint src",
    "build": "babel src -o lib"
}

You could run them in parallel like this:

$ npm-run-all --parallel lint build

See this question for how to run multiple npm commands sequentially



来源:https://stackoverflow.com/questions/47957124/how-can-i-run-multiple-npm-scripts-at-the-same-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!