about:benjie

Random learnings and other thoughts from an unashamed geek

Switching to Yarn Workspaces: An Example

| Comments

Wow, it’s been a fair while since I last blogged! Well: let’s get straight to it!

Yesterday I discovered that yarn (the alternative npm client from Facebook) had introduced a new feature called yarn workspaces which makes working with monorepos a lot easier. I’m working on a couple of projects recently that use the monorepo approach (one is a client project in which I introduced the monorepo approach to share code between React, React-Native and Chrome extension apps easily; the other is the Graphile OSS project I’m currently working on). We’ve had a few minor irritations working with lerna, the most notable of which is that when we want to install a new dependency we can’t just yarn install it - instead we have to add it to package.json and run lerna bootstrap again. At first this wasn’t so bad, but it quickly becomes a chore!

Upon reading the announcement it immediately sounded like yarn workspaces would solve this problem (and more!) so I decided to get on board!

So, without further ado, here’s how I switched graphile-build to yarn workspaces (still with lerna):

First we must install v2.0.0 of lerna and v0.28+ of yarn; currently yarn@latest is 0.27.5, so:

1
npm i -g lerna@latest yarn@rc

Now, since yarn workspaces is experimental we must manually enable this feature:

1
yarn config set workspaces-experimental true

We must also enable useWorkspaces in our lerna.json - open it up and add the following:

1
  "useWorkspaces": true,

We also need to declare that this project uses yarn workspaces (which is independent of Lerna), so in package.json ensure that private is set to true, and add the workspaces key:

1
2
3
4
  "private": true,
  "workspaces": [
    "packages/*"
  ],

Now we’re pretty much ready to go, so let’s clear out the old stuff and start afresh:

1
2
3
4
rm -Rf node_modules packages/*/node_modules
rm -Rf yarn.lock packages/*/yarn.lock
yarn
lerna bootstrap

And that’s it! Enjoy your new workspaces-enabled monorepo!

You can see the changes I made in this pull request:

https://github.com/graphile/graphile-build/pull/36

Comments