How to Run Multiple NPM Scripts in Parallel

Updated: December 28, 2023 By: Guest Contributor Post a comment

This concise, practical article will walk you through some different approaches to running multiple npm scripts in parallel.

Using npm-run-all

Solution description: npm-run-all is a CLI tool to run multiple npm-scripts in parallel or sequential. It is a simple and efficient solution to execute multiple scripts simultaneously without needing to handle the complexity of the underlying process management.

  • Install npm-run-all by running npm install --save-dev npm-run-all.
  • Add a new script in your package.json that uses npm-run-all to run your target scripts in parallel using the run-p command.
  • Execute your new script using npm run.
{
  "scripts": {
    "script1": "echo 'Running script1'",
    "script2": "echo 'Running script2'",
    "parallel": "npm-run-all --parallel script1 script2"
  }
}

Pros: Simplifies running tasks in parallel; has additional features like sequential execution; reduces the need to understand complex shell syntax; cross-platform.

Cons: Requires an additional package; may not be as performant as native shell scripting in certain scenarios.

Using Concurrently

Solution description: concurrently is an npm package that allows you to run multiple commands concurrently. It is a powerful tool that can also manage prefixing output and controlling colors, making it easier to distinguish between script outputs.

  • Install concurrently by running npm install --save-dev concurrently.
  • Create separate scripts for each task in your package.json and a collective script that uses concurrently to run them at the same time.
  • Run the collective script using npm run.
{
  "scripts": {
    "script1": "echo 'Running script1'",
    "script2": "echo 'Running script2'",
    "parallel": "concurrently \"npm run script1\" \"npm run script2\""
  }
}

Pros: Allows for detailed output configuration; can run any command not just npm scripts; cross-platform support.

Cons: More configuration may be necessary for complex tasks; an additional package is required.

Using Native Shell Commands

Solution description: Native shell commands such as the Unix & operator or Windows’ start command can be used to run npm scripts in parallel. This method uses the capabilities of the operating system’s shell environment.

  • Determine the shell you are using and the compatible command for running processes in the background.
  • Add a script in your package.json that leverages the shell’s syntax for running scripts simultaneously.
  • Execute this script using npm run.
{
  "scripts": {
    "script1": "echo 'Running script1'",
    "script2": "echo 'Running script2'",
    "parallel": "npm run script1 & npm run script2 &"
  }
}

Pros: No additional packages needed; uses shell’s native functionality; generally fast execution.

Cons: Platform-dependent; can become complex with multiple scripts; less readable and maintainable; lacks the additional features of dedicated packages like npm-run-all or concurrently.