Run multiple commands concurrently.
Like npm run watch-js & npm run watch-less
but better.
Table of Contents
I like task automation with npm
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
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):
npm | Yarn | pnpm | Bun | |
---|---|---|---|---|
Global | npm i -g concurrently |
yarn global add concurrently |
pnpm add -g concurrently |
bun add -g concurrently |
Local* | npm i -D concurrently |
yarn add -D concurrently |
pnpm add -D concurrently |
bun add -d concurrently |
* It's recommended to add concurrently to devDependencies
as it's usually used for developing purposes. Please adjust the command if this doesn't apply in your case.
Note The
concurrently
command is now also available under the shorthand aliasconc
.
The tool is written in Node.js, but you can use it to run any commands.
Remember to surround separate commands with quotes:
concurrently "command1 arg" "command2 arg"
Otherwise concurrently would try to run 4 separate commands:
command1
, arg
, command2
, arg
.
In package.json, escape quotes:
"start": "concurrently \"command1 arg\" \"command2 arg\""
NPM run commands can be shortened:
concurrently "npm:watch-js" "npm:watch-css" "npm:watch-node"
# Equivalent to:
concurrently -n watch-js,watch-css,watch-node "npm run watch-js" "npm run watch-css" "npm run watch-node"
NPM shortened commands also support wildcards. Given the following scripts in package.json:
{
//...
"scripts": {
// ...
"watch-js": "...",
"watch-css": "...",
"watch-node": "..."
// ...
}
// ...
}
concurrently "npm:watch-*"
# Equivalent to:
concurrently -n js,css,node "npm run watch-js" "npm run watch-css" "npm run watch-node"
# Any name provided for the wildcard command will be used as a prefix to the wildcard
# part of the script name:
concurrently -n w: npm:watch-*
# Equivalent to:
concurrently -n w:js,w:css,w:node "npm run watch-js" "npm run watch-css" "npm run watch-node"
Exclusion is also supported. Given the following scripts in package.json:
{
// ...
"scripts": {
"lint:js": "...",
"lint:ts": "...",
"lint:fix:js": "...",
"lint:fix:ts": "..."
// ...
}
// ...
}
# Running only lint:js and lint:ts
# with lint:fix:js and lint:fix:ts excluded
concurrently "npm:lint:*(!fix)"
Good frontend one-liner example here.
concurrently [options] <command ...>
options | description |
---|---|
-h, --help |
Show help |
-v, -V, --version |
Show version number |
-m, --max-processes |
How many processes should run at once. Exact number or a percent of CPUs available (for example "50%"). New processes only spawn after all restart tries of a process. |
-n, --names |
List of custom names to be used in prefix template. |
--name-separator |
The character to split on. |
-s, --success |
Which command(s) must exit with code 0 in order for concurrently exit with code 0 too. |
-r, --raw |
Output only raw output of processes, disables prettifying and concurrently coloring. |
--no-color |
Disables colors from logging |
--hide |
Comma-separated list of processes to hide the output. The processes can be identified by their name or index. |
-g, --group |
Order the output as if the commands were run sequentially. |
--timings |
Show timing information for all processes. |
-P, --passthrough-arguments |
Passthrough additional arguments to commands (accessible via placeholders) instead of treating them as commands. |
options | description |
---|---|
-p, --prefix |
Prefix used in logging for each process Possible values: index, pid, time, command, name, none, or a template. Example template: "{time}-{pid}" |
-c, --prefix-colors |
Comma-separated list of chalk colors to use on prefixes. If there are more commands than colors, the last color will be repeated. - Available modifiers: reset, bold, dim, italic,underline, inverse, hidden, strikethrough - Available colors: black, red, green, yellow, blue,magenta, cyan, white, gray,any hex values for colors (e.g. #23de43) or auto for an automatically picked color - Available background colors: bgBlack , bgRed ,bgGreen , bgYellow , bgBlue , bgMagenta , bgCyan , bgWhite See https://www.npmjs.com/package/chalk for more information. |
-l, --prefix-length |
Limit how many characters of the command is displayed in prefix. The option can be used to shorten the prefix when it is set to "command" |
-t, --timestamp-format |
Specify the timestamp in moment/date-fns format. |
options | description |
---|---|
-i, --handle-input |
Whether input should be forwarded to the child processes. See examples for more information. |
--default-input-target |
Identifier for child process to which input on stdin should be sent if not specified at start of input. Can be either the index or the name of the process. |
options | description |
---|---|
-k, --kill-others |
Kill other processes if one exits or dies. |
--kill-others-on-fail |
Kill other processes if one exits with non zero status code. |
--kill-signal |
Signal to send to other processes if one exits or dies. (SIGTERM/SIGKILL, defaults to SIGTERM) |
options | description |
---|---|
--restart-tries |
How many times a process that died should restart. Negative numbers will make the process restart forever. |
--restart-after |
Delay time to respawn the process, in milliseconds. |
- Output nothing more than stdout+stderr of child processes
$ concurrently --raw "npm run watch-less" "npm run watch-js"
- Normal output but without colors e.g. when logging to file
$ concurrently --no-color "grunt watch" "http-server" > log
- Custom prefix
$ concurrently --prefix "{time}-{pid}" "npm run watch" "http-server"
- Custom names and colored prefixes
$ concurrently --names "HTTP,WATCH" -c "bgBlue.bold,bgMagenta.bold"
"http-server" "npm run watch"
- Auto varying colored prefixes
$ concurrently -c "auto" "npm run watch" "http-server"
- Mixing auto and manual colored prefixes
$ concurrently -c "red,auto" "npm run watch" "http-server" "echo hello"
- Configuring via environment variables with CONCURRENTLY_ prefix
$ CONCURRENTLY_RAW=true CONCURRENTLY_KILL_OTHERS=true concurrently "echo
hello" "echo world"
- Send input to default
$ concurrently --handle-input "nodemon" "npm run watch-js"
rs # Sends rs command to nodemon process
- Send input to specific child identified by index
$ concurrently --handle-input "npm run watch-js" nodemon
1:rs
- Send input to specific child identified by name
$ concurrently --handle-input -n js,srv "npm run watch-js" nodemon
srv:rs
- Shortened NPM run commands
$ concurrently npm:watch-node npm:watch-js npm:watch-css
- Shortened NPM run command with wildcard (make sure to wrap it in quotes!)
$ concurrently "npm:watch-\*"
- Exclude patterns so that between "lint:js" and "lint:fix:js", only "lint:js" is ran
$ concurrently "npm:*(!fix)"
- Passthrough some additional arguments via '{}' placeholder
$ concurrently -P "echo {1}" -- foo
- Passthrough all additional arguments via '{@}' placeholder
$ concurrently -P "npm:dev-\* -- {@}" -- --watch --noEmit
- Passthrough all additional arguments combined via '{*}' placeholder
$ concurrently -P "npm:dev-_ -- {_}" -- --watch --noEmit
For more details, visit https://github.com/open-cli-tools/concurrently
-
Process exited with code null?
From Node child_process documentation,
exit
event: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, pnpm, or Bun?
Yes! In all examples above, you may replace "
npm
" with "yarn
", "pnpm
", or "bun
".