peerDependencies configuration for publishable libraries with NX & Angular
Asked Answered
C

1

13

I have a monorepo using NX & Angular and I am publishing some libs to NPM and i have the following scenario

apps
   only_App
libs
   lib_A // v1.0.0
   lib_B // v1.0.0
   lib_C // v1.0.0

lib_A depends on lib_B and lib_C.

In the pre-built package.json of lib_A doesn’t have any peerDependency and after i build it prior publishing it, automatically adds the peerDependencies like

// BUILT lib_A/package.json

{
...,
"peerDependencies":{
   "@myScope/lib-b": "1.0.0", //specifically that version
   "@myScope/lib-c": "1.0.0",
}
}

The problem is when i publish lib_A and use it in another project (outside NX and this repo). It will always expect me to use lib_B and lib_C v1.0.0, if I make a patch in lib_B upgrading it to v1.0.1, then im forced to also rebuild lib_A updating the peerDependency version. publish lib_A and upgrade lib_A's version too.

Is there a way i can configure this so when i build lib_A adds the ^ to my libs? i.e

// BUILT lib_A/package.json

{
...,
"peerDependencies":{
   "@myScope/lib-b": "^1.0.0", //automatically includes the ^ (caret)
   "@myScope/lib-c": "^1.0.0",
}
}

Allowing me in this way if I have a minor patch in lib_B, just to update lib_B and not lib_A as well.

So far the only “solution” is to manually copy and paste the peer dependencies generated in the built package.json in the pre-built package.json and add the ^ manually, but i know this is not scalable

Conglobate answered 16/6, 2022 at 12:12 Comment(1)
running into this too - i think the only option is to script it manually as you describeAccrue
T
-3

Add "buildableProjectDepsInPackageJsonType": "dependencies" in your build target in your project.json file.

https://nx.dev/packages/js/executors/tsc#buildableprojectdepsinpackagejsontype

Twink answered 7/9, 2022 at 11:55 Comment(1)
This is probably not desirable - peerDependencies and dependencies are semantically different. Setting this to dependencies will risk a project installing your libs having multiple copies of the target dependencies installed, which can lead to lots of issues where singletons are expected.Duotone

© 2022 - 2024 — McMap. All rights reserved.