Open In App

How to Run Multiple NPM Scripts in Parallel?

Last Updated : 05 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Running multiple npm scripts in parallel can be particularly useful when you need to perform several tasks simultaneously, such as starting a server, running a build process, and monitoring file changes. This can save time and streamline your development workflow. Here’s a comprehensive guide on how to run multiple npm scripts in parallel.

Method 1: npm-run all package

We can use the” npm-run all” package to run different scripts at the same time. First, we have to install the package itself by using the command.

npm install npm-run-all — save-dev

After installation of the package we have to navigate to the package.json file of the project, and we can see that there are two operations listed inside the “scripts” i.e “start” & “build” that we need in order to host the project on the server and to run the build operation simultaneously.

Now, our next step would be to open up a terminal on Mac or command prompt on Windows and “cd” into the project directory and type the command “./node_modules/.bin/npm-run-all build start ” and press Enter.

In the local installation case which we did, npm-run-all will be installed into our project’s node_modules directory. PATH environment variable does not include there, so we have to use

./node_modules/.bin/npm-run-all 
(or $(npm bin)/npm-run-all)  to run npm-run-all command.

Output:

Build operation executed successfully

The app is hosted successfully on the server

Browser Output: Therefore, we can now see that our app is successfully hosted on a local server, and the build operation executed perfectly with the help of a single package “npm-run-all”.

Method 2: Using Concurrently package

In this approach, we will be using the Concurrently package . Using this package we can combine different script commands such as “npm run start” and “npm run build ” into a single script and then run it in the command line.

First, install the package in your project directory using this command :

npm install concurrently --save

Again after installation of the package we have to navigate to the package.json file of the project, and we can see that there are two operations listed inside the “scripts” i.e “start” & “build”  that we need in order to host the project on the server and to run the build operation simultaneously.  Now we have to include a dev script inside the scripts in the package.json file which will hold our different commands combined.

Note: With –kill-others switch, all commands are killed if one dies. We can follow this to create our own dev script :

"dev": "concurrently \"command1 arg\" \"command2 arg\""

Now we can simply run the whole command by simply using:

npm run dev

Method 3: Using concurrently

The concurrently package is another powerful tool to run multiple commands simultaneously. It also provides better output management by prefixing logs with the script name.

Install concurrently

npm install concurrently --save-dev

Configure Your package.json, Add a script to run commands concurrently

"scripts": {
    "script1": "echo 'Running script 1' && sleep 1",
    "script2": "echo 'Running script 2' && sleep 2",
    "script3": "echo 'Running script 3' && sleep 3",
    "parallel": "concurrently \"npm run script1\" \"npm run script2\" \"npm run script3\""
}

Run Scripts in Parallel, Execute the parallel script to run the commands concurrently

npm run parallel



Next Article

Similar Reads