Sling Academy
Home/Node.js/How to Run Multiple NPM Scripts in Parallel

How to Run Multiple NPM Scripts in Parallel

Last updated: December 28, 2023

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.

Next Article: How to Run Multiple NPM Scripts Sequentially

Previous Article: Express.js vs Nest.js: Which is better to build backend APIs?

Series: The First Steps to Node.js

Node.js

You May Also Like

  • NestJS: How to create cursor-based pagination (2 examples)
  • Cursor-Based Pagination in SequelizeJS: Practical Examples
  • MongooseJS: Cursor-Based Pagination Examples
  • Node.js: How to get location from IP address (3 approaches)
  • SequelizeJS: How to reset auto-increment ID after deleting records
  • SequelizeJS: Grouping Results by Multiple Columns
  • NestJS: Using Faker.js to populate database (for testing)
  • NodeJS: Search and download images by keyword from Unsplash API
  • NestJS: Generate N random users using Faker.js
  • Sequelize Upsert: How to insert or update a record in one query
  • NodeJS: Declaring types when using dotenv with TypeScript
  • Using ExpressJS and Multer with TypeScript
  • NodeJS: Link to static assets (JS, CSS) in Pug templates
  • NodeJS: How to use mixins in Pug templates
  • NodeJS: Displaying images and links in Pug templates
  • ExpressJS + Pug: How to use loops to render array data
  • ExpressJS: Using MORGAN to Log HTTP Requests
  • NodeJS: Using express-fileupload to simply upload files
  • ExpressJS: How to render JSON in Pug templates