concurrently

Run commands concurrently. Like `npm run watch-js & npm run watch-less` but...

README

concurrently


Latest Release License Weekly Downloads on NPM CI Status Coverage Status

Run multiple commands concurrently.
Like npm run watch-js & npm run watch-less but better.

Demo

Table of Contents

  - Why
  - Usage
  - API
    - [concurrently(commands[, options])](#concurrentlycommands-options)
    - [Command](#command)
    - [CloseEvent](#closeevent)
  - FAQ

Why


but the usual way to run multiple commands concurrently is
npm run watch-js & npm run watch-css. That's fine but it's hard to keep
on track of different outputs. Also if one process fails, others still keep running
and you won't even notice the difference.

Another option would be to just run all commands in separate terminals. I got
tired of opening terminals and made concurrently.

Features:

- Cross platform (including Windows)
- Output is easy to follow with prefixes
- With --kill-others switch, all commands are killed if one dies
- Spawns commands with spawn-command

Installation


concurrently can be installed in the global scope (if you'd like to have it available and use it on the whole system) or locally for a specific package (for example if you'd like to use it in the scripts section of your package):

|Yarnpnpm
---------
**Global**`npm`yarn
**Local**\*`npm`yarn

\* It's recommended to add **concurrently** as `devDependencies` as it's usually used for developing purposes. Please change this flag if this doesn't apply in your case.

Usage


Note

The concurrently command is now also available under the shorthand alias conc.


The tool is written in Node.js, but you can use it to run any commands.

Remember to surround separate commands with quotes:

  1. ```bash
  2. concurrently "command1 arg" "command2 arg"
  3. ```

Otherwise concurrently would try to run 4 separate commands:
command1, arg, command2, arg.

In package.json, escape quotes:

  1. ```bash
  2. "start": "concurrently \"command1 arg\" \"command2 arg\""
  3. ```

NPM run commands can be shortened:

  1. ```bash
  2. concurrently "npm:watch-js" "npm:watch-css" "npm:watch-node"

  3. # Equivalent to:
  4. concurrently -n watch-js,watch-css,watch-node "npm run watch-js" "npm run watch-css" "npm run watch-node"
  5. ```

NPM shortened commands also support wildcards. Given the following scripts in
package.json:

  1. ```jsonc
  2. {
  3.   //...
  4.   "scripts": {
  5.     // ...
  6.     "watch-js": "...",
  7.     "watch-css": "...",
  8.     "watch-node": "..."
  9.     // ...
  10.   }
  11.   // ...
  12. }
  13. ```

  1. ```bash
  2. concurrently "npm:watch-*"

  3. # Equivalent to:
  4. concurrently -n js,css,node "npm run watch-js" "npm run watch-css" "npm run watch-node"

  5. # Any name provided for the wildcard command will be used as a prefix to the wildcard
  6. # part of the script name:
  7. concurrently -n w: npm:watch-*

  8. # Equivalent to:
  9. concurrently -n w:js,w:css,w:node "npm run watch-js" "npm run watch-css" "npm run watch-node"
  10. ```

Exclusion is also supported. Given the following scripts in package.json:

  1. ```jsonc
  2. {
  3.   // ...
  4.   "scripts": {
  5.     "lint:js": "...",
  6.     "lint:ts": "...",
  7.     "lint:fix:js": "...",
  8.     "lint:fix:ts": "..."
  9.     // ...
  10.   }
  11.   // ...
  12. }
  13. ```

  1. ```bash
  2. # Running only lint:js and lint:ts
  3. #   with lint:fix:js and lint:fix:ts excluded
  4. concurrently "npm:lint:*(!fix)"
  5. ```

Good frontend one-liner example here.

Help:

  1. ```
  2. concurrently [options] <command ...>

  3. General
  4.   -m, --max-processes   How many processes should run at once.
  5.                         Exact number or a percent of CPUs available (for example "50%").
  6.                         New processes only spawn after all restart tries
  7.                         of a process.                            [string]
  8.   -n, --names                  List of custom names to be used in prefix
  9.                         template.
  10.                         Example names: "main,browser,server"     [string]
  11.       --name-separator         The character to split <names> on. Example usage:
  12.                         -n "styles|scripts|server" --name-separator "|"
  13.                         [default: ","]
  14.   -s, --success         Which command(s) must exit with code 0 in order
  15.                         for concurrently exit with code 0 too. Options
  16.                         are:
  17.                         - "first" for the first command to exit;
  18.                         - "last" for the last command to exit;
  19.                         - "all" for all commands;
  20.                         - "command-{name}"/"command-{index}" for the
  21.                         commands with that name or index;
  22.                         - "!command-{name}"/"!command-{index}" for all
  23.                         commands but the ones with that name or index.
  24.                         [default: "all"]
  25.   -r, --raw                    Output only raw output of processes, disables
  26.                         prettifying and concurrently coloring.  [boolean]
  27.       --no-color               Disables colors from logging            [boolean]
  28.       --hide                   Comma-separated list of processes to hide the
  29.                         output.
  30.                         The processes can be identified by their name or
  31.                         index.                     [string] [default: ""]
  32.   -g, --group                  Order the output as if the commands were run
  33.                         sequentially.                           [boolean]
  34.       --timings                Show timing information for all processes.
  35.                         [boolean] [default: false]
  36.   -P, --passthrough-arguments  Passthrough additional arguments to commands
  37.                         (accessible via placeholders) instead of treating
  38.                         them as commands.      [boolean] [default: false]

  39. Prefix styling
  40.   -p, --prefix            Prefix used in logging for each process.
  41.                           Possible values: index, pid, time, command, name,
  42.                           none, or a template. Example template: "{time}-{pid}"
  43.                          [string] [default: index or name (when --names is set)]
  44.   -c, --prefix-colors     Comma-separated list of chalk colors to use on
  45.                           prefixes. If there are more commands than colors, the
  46.                           last color will be repeated.
  47.                           - Available modifiers: reset, bold, dim, italic,
  48.                           underline, inverse, hidden, strikethrough
  49.                           - Available colors: black, red, green, yellow, blue,
  50.                           magenta, cyan, white, gray,
  51.                           any hex values for colors (e.g. #23de43) or auto for
  52.                           an automatically picked color
  53.                           - Available background colors: bgBlack, bgRed,
  54.                           bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
  55.                           See https://www.npmjs.com/package/chalk for more
  56.                           information.               [string] [default: "reset"]
  57.   -l, --prefix-length     Limit how many characters of the command is displayed
  58.                           in prefix. The option can be used to shorten the
  59.                           prefix when it is set to "command"
  60.                                                    [number] [default: 10]
  61.   -t, --timestamp-format  Specify the timestamp in moment/date-fns format.
  62.                             [string] [default: "yyyy-MM-dd HH:mm:ss.SSS"]

  63. Input handling
  64.   -i, --handle-input          Whether input should be forwarded to the child
  65.                               processes. See examples for more information.
  66.                                                     [boolean]
  67.       --default-input-target  Identifier for child process to which input on
  68.                               stdin should be sent if not specified at start of
  69.                               input.
  70.                               Can be either the index or the name of the
  71.                               process.                              [default: 0]

  72. Killing other processes
  73.   -k, --kill-others          Kill other processes if one exits or dies.[boolean]
  74.       --kill-others-on-fail  Kill other processes if one exits with non zero
  75.                              status code.                              [boolean]

  76. Restarting
  77.       --restart-tries  How many times a process that died should restart.
  78.                        Negative numbers will make the process restart forever.
  79.                        [number] [default: 0]
  80.       --restart-after  Delay time to respawn the process, in milliseconds.
  81.                        [number] [default: 0]

  82. Options:
  83.   -h, --help         Show help                                  [boolean]
  84.   -v, -V, --version  Show version number                        [boolean]


  85. Examples:

  86. - Output nothing more than stdout+stderr of child processes

  87.      $ concurrently --raw "npm run watch-less" "npm run watch-js"

  88. - Normal output but without colors e.g. when logging to file

  89.      $ concurrently --no-color "grunt watch" "http-server" > log

  90. - Custom prefix

  91.      $ concurrently --prefix "{time}-{pid}" "npm run watch" "http-server"

  92. - Custom names and colored prefixes

  93.      $ concurrently --names "HTTP,WATCH" -c "bgBlue.bold,bgMagenta.bold"
  94.      "http-server" "npm run watch"

  95. - Auto varying colored prefixes

  96.      $ concurrently -c "auto" "npm run watch" "http-server"

  97. - Mixing auto and manual colored prefixes

  98.      $ concurrently -c "red,auto" "npm run watch" "http-server" "echo hello"

  99. - Configuring via environment variables with CONCURRENTLY_ prefix

  100.      $ CONCURRENTLY_RAW=true CONCURRENTLY_KILL_OTHERS=true concurrently "echo
  101.      hello" "echo world"

  102. - Send input to default

  103.      $ concurrently --handle-input "nodemon" "npm run watch-js"
  104.      rs  # Sends rs command to nodemon process

  105. - Send input to specific child identified by index

  106.      $ concurrently --handle-input "npm run watch-js" nodemon
  107.      1:rs

  108. - Send input to specific child identified by name

  109.      $ concurrently --handle-input -n js,srv "npm run watch-js" nodemon
  110.      srv:rs

  111. - Shortened NPM run commands

  112.      $ concurrently npm:watch-node npm:watch-js npm:watch-css

  113. - Shortened NPM run command with wildcard (make sure to wrap it in quotes!)

  114.      $ concurrently "npm:watch-*"

  115. - Exclude patterns so that between "lint:js" and "lint:fix:js", only "lint:js"
  116. is ran

  117.      $ concurrently "npm:*(!fix)"

  118. - Passthrough some additional arguments via '{<number>}' placeholder

  119.      $ concurrently -P "echo {1}" -- foo

  120. - Passthrough all additional arguments via '{@}' placeholder

  121.      $ concurrently -P "npm:dev-* -- {@}" -- --watch --noEmit

  122. - Passthrough all additional arguments combined via '{*}' placeholder

  123.      $ concurrently -P "npm:dev-* -- {*}" -- --watch --noEmit

  124. For more details, visit https://github.com/open-cli-tools/concurrently
  125. ```

API


concurrently can be used programmatically by using the API documented below:

concurrently(commands[, options])


- commands: an array of either strings (containing the commands to run) or objects
  with the shape { command, name, prefixColor, env, cwd }.

- options (optional): an object containing any of the below:
  - cwd: the working directory to be used by all commands. Can be overriden per command.
    Default: process.cwd().
  - defaultInputTarget: the default input target when reading from inputStream.
    Default: 0.
  - handleInput: when true, reads input from process.stdin.
  - inputStream: a [Readable stream](https://nodejs.org/dist/latest-v10.x/docs/api/stream.html#stream_readable_streams)
    to read the input from. Should only be used in the rare instance you would like to stream anything other than process.stdin. Overrides handleInput.
  - pauseInputStreamOnFinish: by default, pauses the input stream (process.stdin when handleInput is enabled, or inputStream if provided) when all of the processes have finished. If you need to read from the input stream after concurrently has finished, set this to false. (#252).
  - killOthers: an array of exitting conditions that will cause a process to kill others.
    Can contain any of success or failure.
  - maxProcesses: how many processes should run at once.
  - outputStream: a [Writable stream](https://nodejs.org/dist/latest-v10.x/docs/api/stream.html#stream_writable_streams)
    to write logs to. Default: process.stdout.
  - prefix: the prefix type to use when logging processes output.
    Possible values: index, pid, time, command, name, none, or a template (eg [{time} process: {pid}]).
    Default: the name of the process, or its index if no name is set.
  - prefixColors: a list of colors as supported by chalk orauto for an automatically picked color.
    If concurrently would run more commands than there are colors, the last color is repeated, unless if the last color value is auto which means following colors are automatically picked to vary.
    Prefix colors specified per-command take precedence over this list.
  - prefixLength: how many characters to show when prefixing with command. Default: 10
  - raw: whether raw mode should be used, meaning strictly process output will
    be logged, without any prefixes, coloring or extra stuff.
  - successCondition: the condition to consider the run was successful.
    If first, only the first process to exit will make up the success of the run; if last, the last process that exits will determine whether the run succeeds.
    Anything else means all processes should exit successfully.
  - restartTries: how many attempts to restart a process that dies will be made. Default: 0.
  - restartDelay: how many milliseconds to wait between process restarts. Default: 0.
  - timestampFormat: a date-fns format
    to use when prefixing with time. Default: yyyy-MM-dd HH:mm:ss.ZZZ
  - additionalArguments: list of additional arguments passed that will get replaced in each command. If not defined, no argument replacing will happen.

Returns: an object in the shape { result, commands }.

>

- result: a Promise that resolves if the run was successful (according to successCondition option),

or rejects, containing an array of [CloseEvent](#CloseEvent), in the order that the commands terminated.

- commands: an array of all spawned [Commands](#Command).


Example:

  1. ```js
  2. const concurrently = require('concurrently');
  3. const { result } = concurrently(
  4.   [
  5.     'npm:watch-*',
  6.     { command: 'nodemon', name: 'server' },
  7.     { command: 'deploy', name: 'deploy', env: { PUBLIC_KEY: '...' } },
  8.     {
  9.       command: 'watch',
  10.       name: 'watch',
  11.       cwd: path.resolve(__dirname, 'scripts/watchers'),
  12.     },
  13.   ],
  14.   {
  15.     prefix: 'name',
  16.     killOthers: ['failure', 'success'],
  17.     restartTries: 3,
  18.     cwd: path.resolve(__dirname, 'scripts'),
  19.   }
  20. );
  21. result.then(success, failure);
  22. ```

Command


An object that contains all information about a spawned command, and ways to interact with it.
It has the following properties:

- index: the index of the command among all commands spawned.
- command: the command line of the command.
- name: the name of the command; defaults to an empty string.
- cwd: the current working directory of the command.
- env: an object with all the environment variables that the command will be spawned with.
- killed: whether the command has been killed.
- exited: whether the command exited yet.
- pid: the command's process ID.
- stdin: a Writable stream to the command's stdin.
- stdout: an RxJS observable to the command's stdout.
- stderr: an RxJS observable to the command's stderr.
- error: an RxJS observable to the command's error events (e.g. when it fails to spawn).
- timer: an RxJS observable to the command's timing events (e.g. starting, stopping).
- close: an RxJS observable to the command's close events.
  See [CloseEvent](#CloseEvent) for more information.
- start(): starts the command, setting up all
- kill([signal]): kills the command, optionally specifying a signal (e.g. SIGTERM, SIGKILL, etc).

CloseEvent


An object with information about a command's closing event.
It contains the following properties:

- command: a stripped down version of [Command](#command), including only name, command, env and cwd properties.
- index: the index of the command among all commands spawned.
- killed: whether the command exited because it was killed.
- exitCode: the exit code of the command's process, or the signal which it was killed with.
- timings: an object in the shape { startDate, endDate, durationSeconds }.

FAQ


- Process exited with code _null_?


  > This event is emitted after the child process ends. If the process
  > terminated normally, code is the final exit code of the process,
  > otherwise null. If the process terminated due to receipt of a signal,
  > signal is the string name of the signal, otherwise null.

  So _null_ means the process didn't terminate normally. This will make concurrently
  to return non-zero exit code too.

- Does this work with the npm-replacements yarn or pnpm?

  Yes! In all examples above, you may replace "npm" with "yarn" or "pnpm".