Difficulty with new monorepo deployment options

Some time ago I experimented with Yarn Workspaces and Netlify’s recently released better support for monorepos that @fool mentioned (netlify.toml per site in subdirectory). I discovered a setup that might solve some of the above mentioned problems.

Assumptions:

  • You use Yarn Workspaces with more than one site (or one site in subdirectory),
  • You set your base dir in Netlify UI to e.g. packages/website to use per-site netlify.toml file
  • You want Netlify to install dependencies using yarn (without additional commands, changing directory or installing first with NPM and then with Yarn)
  • You want Netlify to cache dependencies between builds

So, here’s the fun part. Based on facts that:

  • You can run yarn install in a Workspace directory instead of project root
  • Netlify requires yarn.lock to exists in a base directory to use Yarn instead of NPM

You can set up your project like this:

├── packages
│   ├── frontend
│   │   └── index.js
│   └── website
│       ├── index.html
│       ├── main.js
│       ├── netlify.toml
│       ├── package.json
│       └── yarn.lock    # Empty file to trigger yarn install
├── package.json
└── yarn.lock            # Real yarn.lock

And Netlify will pick up your netlify.toml and install dependencies with Yarn :sunglasses:

Unfortunately, this does not eliminate the problem with proper caching of node_modules/. Fortunately, Yarn uses local cache directory that Netlify persists between builds. That speeds up things a bit. But for the issue to be fully resolved we have to wait for a native solution in build image.

I hope this will help you :slight_smile:

EDIT: I originaly just posted a link to my comment in GH issue (Support for monorepos, including lerna or yarn workspaces · Issue #196 · netlify/build-image · GitHub) but decided to copy it here.

2 Likes