How do I update all subfolder package.json in a monorepo to the same version?
Asked Answered
L

1

10

I have a repository with a bunch of different front-end React projects, which all have package.json files of their own.

I have four requrements:

  1. I want to keep all of these projects on the same dependency track. (ie, I want all my projects to use the same versions of each npm package)
  2. I want to be able to update every package.json to their latest version, with one "global" command.
  3. Ideally, I'd like to use some sort of proven open-source tool like Yarn Workspaces or Lerna, instead of managing this all myself with a global package.json file.
  4. I'm using the Yarn ecosystem.

These packages do not need to run concurrently. Not every project contains the same exact dependencies, but all contain at least a few common dependencies (ie React, Webpack, Babel, etc). These packages are also entirely private and should not be published on npm, only on GitHub.

I've done some research into the tooling and am confused about if Yarn Workspaces alone is the solution, or if Lerna is necessary. Here's what I've done so far in Yarn Workspaces:

Folder structure:

Repo-root
   Packages
      Project-1
      Project-2

Repo root package.json:

{
   "private": true,
   "name": "example-monorepo",
   "workspaces": ["packages/*"],
   "scripts": {}
}

I understand that Yarn Workspaces tries to move all common dependencies to the root node_modules behind-the-scenes, but I'm confused about how to accomplish the shared dependency installation and updating to the latest versions requirement. Can Yarn Workspaces accomplish the requirements on its own, or is Lerna necessary for this? How would this be accomplished in Lerna? Do I need to define a global package.json "single source of truth", or do the tools do some version of it themselves behind the scenes? How does Lerna or Yarn Workspaces "know" how to update all the individual package.json files to their latest versions without a global set of version ranges? (which I want to be the same for all projects)

Letterpress answered 20/6, 2021 at 19:18 Comment(0)
C
9

Yarn has an upgrade interactive mode that makes this pretty easy.

In your case, you want to cd into the root of your monorepo and run yarn upgrade-interactive --latest

The packages will be broken into two lists: your dependencies and devDependencies, but otherwise sorted alphabetically. You do have to manually select each library you want to upgrade, but all of the same modules will be next to one another regardless of which subdirectory they're in.

Census answered 15/9, 2021 at 14:41 Comment(2)
This does not work; it upgrades yarn.lock bot underlaying package.json files remain untouched.Unread
@Unread on my end, this still works as described. You might verify that: (a) you're using the latest yarn, (b) you're using the --latest flag, (c) while in interactive mode, you're selecting packages that are outdatedCensus

© 2022 - 2024 — McMap. All rights reserved.