37

I'm migrating from NPM to Yarn, and I want to run scripts in parallel such as:

npm-run-all --parallel script1 script2 script3

What is its equivalent in Yarn?

What I found as its equivalent is to run each separately:

yarn run script1 && yarn run script2 && yarn run script3

but I can't run scripts in parallel.

how to use multiple scripts & in parallel?

tk421
  • 5,775
  • 6
  • 23
  • 34
belhadj haythem
  • 622
  • 2
  • 6
  • 16
  • Does this answer your question? [How can I run multiple npm scripts in parallel?](https://stackoverflow.com/questions/30950032/how-can-i-run-multiple-npm-scripts-in-parallel) – Maneet Dec 12 '19 at 20:54

6 Answers6

57

There is a difference between using & and &&. Using & will run scripts in parallel, using && will run scripts one after the other.

package.json:

{
    "parallel": "yarn script1 & yarn script2",
    "serial": "yarn script1 && yarn script2",
    "script1": "... some script here",
    "script2": "... some there script here"
}
Pang
  • 9,564
  • 146
  • 81
  • 122
magikMaker
  • 4,929
  • 1
  • 33
  • 25
  • 3
    This won't work for the newer versions of Yarn (I realise OP doesn't specify which they are using). It has its own shell which, for cross-platform compat reasons won't let you use `&`. It will trigger `Syntax Error: Expected "$", "$(", "${", "&&", "'", ";", "<", "<<<", ">", ">>", "\"", "\\", "|", "|&", "||", [ \t], or end of input but "&" found`. To use `&`, would need to either use a library built for this (as other replies have described), or explicitly execute a bash command like `"bash -c 'yarn script1 & yarn script2'"` – DanCouper Nov 25 '20 at 15:28
  • what's about `yarn script -- --write`? I mean, calling another script and passing arguments for it. – SalahAdDin Jul 31 '22 at 17:17
19

you can use concurrently. E.g.:

concurrently "yarn run script1"   "yarn run script2"   "yarn run script3"
arcuri82
  • 885
  • 1
  • 8
  • 17
18

From what I read on documentation of npm-run-all, you can just keep using it, and, as long as you run the script with yarn it will use YARN to run scripts in parallel.

Here is the original quote from https://github.com/mysticatea/npm-run-all

Yarn Compatibility

If a script is invoked with Yarn, npm-run-all will correctly use Yarn to execute the plan's child scripts.

Community
  • 1
  • 1
raul
  • 620
  • 6
  • 14
13

You can use https://www.npmjs.com/package/yarn-run-all which is made for this purpose.

Edit:

My answer was wrong.

npm-run-all is compatible with yarn:

If a script is invoked with Yarn, npm-run-all will correctly use Yarn to execute the plan's child scripts.

Additionnaly, the yarn-run-all package is linked to repository https://github.com/mysticatea/npm-run-all...

In the end, I don't understand why the yarn-run-all package exists.

Anyways, just use npm-run-all package instead of yarn-run-all.

Merott
  • 7,189
  • 6
  • 40
  • 52
Kevin Struillou
  • 856
  • 10
  • 19
4

Probably a bit off the question, but in case Yarn v2+ with Workspaces is being used, there is a plugin (workspace-tools) that makes it easier to run a script in all of the workspaces's packages (ie: if you have a monorepo and need to run start in all of them):

# Install plugin:
yarn plugin import workspace-tools

# Run script start:dev on all of the packages in parallel
yarn workspaces foreach -p -v -i run start
  • -p: Run the commands in parallel
  • -v: Prefix each output line with the name of the originating workspace
  • -i: Print the output of commands in real-time instead of buffering it. Found this useful as in some cases, no output will be shown.

More info: https://yarnpkg.com/cli/workspaces/foreach

yorch
  • 7,170
  • 8
  • 32
  • 38
0

If your scripts are intended to run continuously in background like daemons, you can use fly-run.

I am using it when I need to spawn multiple processes (yarn serve of multiple micro-services projects in development).

Install it:

yarn global add @ifnot/fly-run

Create a configuration file config.js:

module.exports = [
  {
    name: 'script1',
    command: '/path/to/script1'
  },
  {
    name: 'script1',
    command: '/path/to/script2'
  },
  // more ...
]

Run it using the configuration:

fly-run config.js

Use it :

CTRL+C closes all your spawned process, type restart script1 for restarting one script, more commands and configuration on the github.

Ifnot
  • 4,914
  • 4
  • 33
  • 47