package discovery: npm-run-all

2021-01-05

 | 

~3 min read

 | 

514 words

Another entry in my Package Discovery series. Today’s topic is focus lock using npm-run-all!

This is a little bit of an older project, but while it’s not been updated recently, it’s still working like a charm for me!

npm-run-all addresses a very common use case of scripts that run together.

Imagine the situation where you have a continuous integration script that validates the code is clean by running it through a number of checks. When running these scripts sequentially, it can take a considerable amount of time, but there’s no actual need to run them sequentially since the processes don’t depend on one another.

This is one of the places where npm-run-all comes in handy.

Instead of:

package.json
{
  "scripts": {
    //...
    "validate": "npm run check-types && npm run check-format && npm run lint && npm run build"
  }
}

We can modify it to use npm-run-all and run each command in parallel.

package.json
{
  "scripts": {
    //...
    "validate": "npm-run-all --parllel check-types check-format lint build"
  }
}

While this is a simple use case, npm-run-all has a number of other usage options to dictate how to handle errors, log levels, etc.

Usage:
    $ npm-run-all [--help | -h | --version | -v]
    $ npm-run-all [tasks] [OPTIONS]

    Run given npm-scripts in parallel or sequential.

    <tasks> : A list of npm-scripts' names and Glob-like patterns.

Options:
    --aggregate-output   - - - Avoid interleaving output by delaying printing of
                               each command's output until it has finished.
    -c, --continue-on-error  - Set the flag to continue executing
                               other/subsequent tasks even if a task threw an
                               error. 'npm-run-all' itself will exit with
                               non-zero code if one or more tasks threw error(s)
    --max-parallel <number>  - Set the maximum number of parallelism. Default is
                               unlimited.
    --npm-path <string>  - - - Set the path to npm. Default is the value of
                               environment variable npm_execpath.
                               If the variable is not defined, then it's "npm."
                               In this case, the "npm" command must be found in
                               environment variable PATH.
    -l, --print-label  - - - - Set the flag to print the task name as a prefix
                               on each line of output. Tools in tasks may stop
                               coloring their output if this option was given.
    -n, --print-name   - - - - Set the flag to print the task name before
                               running each task.
    -p, --parallel <tasks>   - Run a group of tasks in parallel.
                               e.g. 'npm-run-all -p foo bar' is similar to
                                    'npm run foo & npm run bar'.
    -r, --race   - - - - - - - Set the flag to kill all tasks when a task
                               finished with zero. This option is valid only
                               with 'parallel' option.
    -s, --sequential <tasks> - Run a group of tasks sequentially.
        --serial <tasks>       e.g. 'npm-run-all -s foo bar' is similar to
                                    'npm run foo && npm run bar'.
                               '--serial' is a synonym of '--sequential'.
    --silent   - - - - - - - - Set 'silent' to the log level of npm.

Examples:
    $ npm-run-all --serial clean lint build:**
    $ npm-run-all --parallel watch:**
    $ npm-run-all clean lint --parallel "build:** -- --watch"
    $ npm-run-all -l -p start-server start-browser start-electron


Hi there and thanks for reading! My name's Stephen. I live in Chicago with my wife, Kate, and dog, Finn. Want more? See about and get in touch!