DEV Community

Cover image for Why Every JavaScript Developer Should Know npm-run-all (Your Life Gets Easier)
Adrian Jiga
Adrian Jiga

Posted on • Edited on

Why Every JavaScript Developer Should Know npm-run-all (Your Life Gets Easier)

If you want a platform-independent way to run multiple npm scripts with a single command, without shell hacks or editor specific tasks, use npm-run-all2. It’s a maintained fork of npm-run-all and a drop-in replacement with the same commands: run-p, run-s, and npm-run-all.

This guide shows practical examples you can paste into package.json.

Why npm-run-all2?

It is cross-platform: works the same on macOS, Linux, and Windows and it has simple shorthands:

  • run-p - run scripts in parallel
  • run-s - run scripts in sequence

Useful flags: labels, glob patterns, race mode, continue-on-error, and max concurrency: --race, --continue-on-error, --max-parallel, --print-label.

Install

If you've never used the original:

npm i -D npm-run-all2
Enter fullscreen mode Exit fullscreen mode

Migrating from npm-run-all:

npm remove npm-run-all
npm i -D npm-run-all2
Enter fullscreen mode Exit fullscreen mode

Usage (in parallel)

Use parallel for independent long-running tasks (e.g., dev servers, watchers). Let's say you have this in your package.json file:

{
  "scripts": {
    "frontend": "cd frontend && npm run dev",
    "backend": "cd backend && npm run start:dev",
    "start:dev": "run-p frontend backend"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, if you run

npm run start:dev
Enter fullscreen mode Exit fullscreen mode

you will have both the backend and the frontend servers up and running.

Usage (in sequence)

Use sequential when you need to run scripts in sequence (e.g. format > lint > compile). Let's say you have this in your package.json file:

{
  "scripts": {
    "format": "prettier --write",
    "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
    "compile": "tsc -p tsconfig.json",
    "build": "run-s format lint compile"
  }
}
Enter fullscreen mode Exit fullscreen mode

Now, if you run

npm run build
Enter fullscreen mode Exit fullscreen mode

you will have basically executed three commands one after the other: npm run format > npm run lint > npm run compile.

More information

You can find more usage patterns and combinations here at these URLs:

Top comments (0)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.