I recently created a monorepo housing three libraries: lib1, lib2, and lib3. The structure is designed such that lib3 depends on lib1 and lib2.
https://i.sstatic.net/5xrqf.png
My goal is to establish local references to lib1 and lib2 within lib3.
Traditionally, I would publish lib1 and lib2 to an npm registry and then update the dependencies in lib3 accordingly to ensure an up-to-date version of lib3. This is reflected in the package.json files as follows:
packages/package.json
{
"name": "packages",
"scripts": {
"build-lib1": "ng build lib1",
"build-lib2": "ng build lib2",
"build-lib3": "ng build lib3"
},
"private": true,
"dependencies": {
"lib1": "1.0.0",
"lib2": "1.0.0",
...
},
"devDependencies": {
...
}
}
packages/lib1/package.json
{
"name": "lib1",
"peerDependencies": {
...
},
"dependencies": {
"tslib": "^2.4.0"
}
}
packages/lib2/package.json
{
"name": "lib2",
"peerDependencies": {
...
},
"dependencies": {
"tslib": "^2.4.0"
}
}
packages/lib3/package.json
{
"name": "lib3",
"peerDependencies": {
...
},
"dependencies": {
"tslib": "^2.4.0",
"lib1": "1.0.0",
"lib2": "1.0.0"
}
}
In this setup, lib1 and lib2 are added as dependencies in lib3, not peerDependencies, to ensure their automatic inclusion as transitive dependencies when installing lib3 in another application. To accomplish this, I needed to include lib and lib2 in allowedNonPeerDependencies
within packages/lib3/ng-package.json
.
The versions of these libraries are controlled by a CI pipeline and are tied to the git tag of the repository. However, this can result in different versions for lib1/lib2 and lib3. For example, if I publish v1.0.0 for lib1 and lib2, and then update the dependencies in the project, lib3 may end up with v1.0.1.
For context, the versions being used are:
- Angular 15
- Typescript 4.8.3
- npm 9.2.0
My current aim is to find a way to directly reference lib1 and lib2 within lib3 without sacrificing the transitive dependency functionality.
What is considered the best approach to achieve this objective? I've looked into npm workspaces and project references in TypeScript but have hit a roadblock and am unsure of the next steps.