Lerna bootstrap prepare in yarn workspaces


prepare is a useful script for running a command before a package is published or on local install.

jq . packages/my-lerna-package/package.json # cat packages/my-lerna-package/package.json
{
  "name": "my-lerna-package",
  "scripts": {
    "prepare": "echo 'called prepare in my-lerna-package'"
  }
}

However, lerna bootstrap doesn’t run prepare when using yarn workspaces (see issue).

yarn lerna bootstrap
yarn run v1.9.4
$ path/to/monorepo/node_modules/.bin/lerna bootstrap
lerna notice cli v3.13.1
lerna info versioning independent
lerna info bootstrap root only
[1/5] πŸ”  Validating package.json...
[2/5] πŸ”  Resolving packages...
success Already up-to-date.
✨  Done in 1.89s.

This is because when useWorkspaces is enabled, lerna bootstrap only runs yarn install. Thus, the problem lies with yarn (see issue).

The workaround is to add a prepare script to your root package.json:

{
  "scripts": {
    "prepare": "lerna run prepare"
  },
  "private": true
}

Now when you run lerna bootstrap:

yarn lerna bootstrap
yarn run v1.9.4
$ path/to/monorepo/node_modules/.bin/lerna bootstrap
lerna notice cli v3.13.1
lerna info versioning independent
lerna info bootstrap root only
[1/5] πŸ”  Validating package.json...
[2/5] πŸ”  Resolving packages...
success Already up-to-date.
$ lerna run prepare
lerna notice cli v3.13.1
lerna info versioning independent
lerna info Executing command in 1 package: "yarn run prepare"
lerna info run Ran npm script 'prepare' in 'my-lerna-package' in 0.4s:
$ echo 'called prepare in my-lerna-package'
called prepare in my-lerna-package
lerna success run Ran npm script 'prepare' in 1 package in 0.4s:
lerna success - my-lerna-package
✨  Done in 2.78s.

You can also set the options --stream and --sort to optimize the current command (see comment):

lerna run --stream --sort prepare


Please support this site and join our Discord!