zaplec
zaplec

Reputation: 1819

How to keep monorepo deps in sync in JS/TS project?

Is there some smart or "industry standard" way to keep dependencies in sync inside workspaces/modules in a monorepo?

Let's say we have following structure in the repository

AwesomeProject/
├─ workspaceA/
│  ├─ packageA.json
├─ workspaceB/
│  ├─ packageB.json
├─ workspaceC/
│  ├─ packageC.json
packageRoot.json

We can install a dependency into a workspace with npm install --save --workspace=workspaceA. Let's assume that we are using some dependency in two or more of our workspaces i.e. axios. At some point the dependency is updated into only one of the workspaces. This happens multiple times during the development along the years and soon we'll end up into a situation where the workspaces are not using the same version of the dependency anymore. As time passes, this might become a big problem and it is really cumbersome to start updating and syncing the packages.

As a solution to this in some projects I have used overrides in the root package.json.

// packageA.json and packageB.json
{
 ...
 dependencies: {
   ...
   axios: "*",
   ...
 }
}
packageRoot.json
{
  ...
  overrides: {
    axios: "1.2.3",
    ...
  }
}

This works fine to some extent. I know this is kind of risky as overrides is overriding also the subdependencies from other dependencies, if they happen to have any overridden dependencies listed as their dependency. The smaller the project, the less likely this conflict is to happen.

However, on the contrary, the bigger the project grows, the more likely you are going to run into problems with this overriding method.

So, is there a way to define the dependencies in a monorepo so that all the workspaces are using the same version of a dependency they are all using without the need to define these overrides? If you update the dependency X it is updated in all workspaces, but at the same time the 3rd party dependencies keep using their defined dependencies unless specifically overriden in the overrides.

There might be different ways to do it depending which package manager is used, so solution for each package manager is welcome. And if there is a solution that suits all the package managers even better!

Upvotes: 0

Views: 38

Answers (0)

Related Questions