In my Typescript monorepo with Javascript packages, each package has its own package.json
file where dependencies are defined. Some external dependencies may be duplicated across multiple packages.
repo/
├── package.json
├── packageA
│ └── package.json
│ dependencies: [
│ "libA": "^1.0.0",
│ "libB": "^1.0.0"
│ ]
├── packageB
│ └── package.json
│ dependencies: [
│ "libC": "^1.0.0"
│ ]
└── packageC
└── package.json
dependencies: [
"libA": "^1.0.0" <-- duplicated with packageA
]
Sometimes, I unintentionally end up with different versions of the same dependency in two packages when running commands like yarn workspace packageC libA
:
repo/
├── package.json
├── packageA
│ └── package.json
│ dependencies: [
│ "libA": "^1.0.0",
│ "libB": "^1.0.0"
│ ]
├── packageB
│ └── package.json
│ dependencies: [
│ "libC": "^1.0.0"
│ ]
└── packageC
└── package.json
dependencies: [
"libA": "^2.0.0" <-- different version than packageA's
]
These discrepancies can lead to subtle bugs in my application, prompting me to seek a way to ensure consistent versions of packages within my monorepo.
I am exploring various solutions for enforcing this consistency:
- Merging shared libraries into the top-level
package.json
. (Will this affect build size if only some packages require a particular library?) - Implementing version checks during the build process to warn about mismatches
- Creating a yarn hook that alerts me when installing conflicting versions via
yarn add
- Utilizing a third-party tool for managing common versions across packages
- Exploring other potential solutions yet to be considered