Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I run multiple npm scripts in parallel?

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.

like image 200
André Pena Avatar asked Jun 20 '15 03:06

André Pena


People also ask

How do I run two npm scripts simultaneously?

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.

Can the npm run all CLI tool run multiple npm scripts in parallel?

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 & .

Does npm install in parallel?

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.

What is concurrently in npm?

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.


2 Answers

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\"" 
like image 94
Neil Kistner Avatar answered Oct 03 '22 04:10

Neil Kistner


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" 
like image 37
Diogo Cardoso Avatar answered Oct 03 '22 03:10

Diogo Cardoso