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",
},
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",
},
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.
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
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
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.
Use concurrently to run multiple npm scripts.
Steps:
Run npm i concurrently
to install concurrently.
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\"" },
Run npm run all
to execute multiple npm scripts.
You can use one &
for parallel run script
"dev": "npm run start-watch & npm run wp-server"