In my package.json
I have these two scripts:
"scripts": { "start-watch": "nodemon run-babel index.js", "wp-server": "webpack-dev-server", }
I have to run these 2 scripts in parallel everytime I start developing in Node.js. The first thing I thought of was adding a third script like this:
"dev": "npm run start-watch && npm run wp-server"
... but that will wait for start-watch
to finish before running wp-server
.
How can I run these in parallel? Please keep in mind that I need to see the output
of these commands. Also, if your solution involves a build tool, I'd rather use gulp
instead of grunt
because I already use it in another project.
A quick way of doing it is npm run start-watch & npm run wp-server . This will run the first command as a background thread. This works really well when one of the commands is not long running and does not need to be manually exited later.
The official npm run-script command cannot run multiple scripts, so if we want to run multiple scripts, it's redundant a bit. Let's shorten it by glob-like patterns. Cross platform. We sometimes use & to run multiple command in parallel, but cmd.exe ( npm run-script uses it by default) does not support the & .
Whenever Yarn or npm need to install a package, they carry out a series of tasks. In npm, these tasks are executed per package and sequentially, meaning it will wait for a package to be fully installed before moving on to the next. In contrast, Yarn executes these tasks in parallel, increasing performance.
Concurrently is a Node package that allows you to run multiple scripts at the same time in Node. js. It's especially useful if you want to run your app's front-end and back-end from a single NPM command.
Use a package called concurrently.
npm i concurrently --save-dev
Then setup your npm run dev
task as so:
"dev": "concurrently --kill-others \"npm run start-watch\" \"npm run wp-server\""
If you're using an UNIX-like environment, just use &
as the separator:
"dev": "npm run start-watch & npm run wp-server"
Otherwise if you're interested on a cross-platform solution, you could use npm-run-all module:
"dev": "npm-run-all --parallel start-watch wp-server"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With