8

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",
},
Pingolin
  • 3,161
  • 6
  • 25
  • 40
bob
  • 753
  • 4
  • 15
  • 27
  • 2
    Does this answer your question? [How can I run multiple npm scripts in parallel?](https://stackoverflow.com/questions/30950032/how-can-i-run-multiple-npm-scripts-in-parallel) – KyleMit Jan 03 '20 at 21:47

7 Answers7

15

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.
danilonet
  • 1,757
  • 16
  • 33
  • It doesn't seems to work with express. I have this `node serverNew.js & ng serve ` and all I get is `app running on port 5000` without continuing to the `ng serve` command – Royi Namir Oct 09 '18 at 11:36
4
"scripts": {
    "sw": "webpack-dev-server & webpack -wd"
},

then

npm run sw
胡亚雄
  • 2,161
  • 1
  • 19
  • 21
4

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

KyleMit
  • 30,350
  • 66
  • 462
  • 664
3

As of now the syntax seems to have changed a little bit you need to pass & within quotes .

Below is the command that I ran for my demo scripts.

for sequential execution :

npm run temp '&&' npm run temp1 

for parallel execution

npm run temp '&' npm run temp1
Meloman
  • 3,558
  • 3
  • 41
  • 51
1

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.
JulianSoto
  • 182
  • 1
  • 4
  • 15
1

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.
Jerry Chong
  • 7,954
  • 4
  • 45
  • 40
0

You can use one & for parallel run script

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

Reference link

Behnam
  • 6,244
  • 1
  • 39
  • 36