Search code examples
javascriptyarnpkglernamonorepoyarn-workspaces

Yarn Link All Packages/Workspaces


Yarn's link allows you to register a local package for symlinking into another local package. To use link you cd into the package directory and run yarn link. This works fine on individual packages, however I have a monorepo, using Yarn's workspaces alongside Lerna. There are many packages, and I would like a simple way of linking all the packages within the monorepo.

My package.json:

…
  "workspaces": [
    "packages/*"
  ],
…

My lerna.json:

{
  "npmClient": "yarn",
  "useWorkspaces": true,
  "packages": [
    "packages/*"
  ],
  …
}

Is there a simple way to run yarn link (and yarn unlink) on each package?


Solution

  • Lerna provides the exec command to 'Run an arbitrary command in each package':

    link.sh

    lerna exec -- yarn link
    

    unlink.sh

    lerna exec -- yarn unlink