问题
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:
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.- 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