Common dependency lib version with yarn workspaces

4k views Asked by At

Assuming I have an external NPM lib dependency that is shared among different workspaces, is it possible to set a single version for all dependant workspaces? What if this dependency is a peer or dependency ?

e.g

// external-lib-dep in npm

//package1's package.json
...
"dependencies": {
   "external-lib-dep": "^1.0.0"
}
...

// package2's package.json
...
"dependencies": {
   "external-lib-dep": "^1.2.0"
}
...

// package3's package.json
...
"dependencies": {
   "very-different-external-lib-dep": "^1.2.0"
}
...

Is it possible to somehow "share" the versions of deps instead of copying it over and over again? (just like dependencyManagement in parent pom in maven).

What if the monorepo holds "common" libs for all services in a certain company. Will they solution work when I use one of the libs in my service?

1

There are 1 answers

4
Viktor Vlasenko On

It is possible to share the versions of dependency among workspaces in one specific way.

Your workspaces should declare external-lib-dep in their peerDependencies. And in your monorepo root package.json you should declare external-lib-dep in dependencies. This way your workspaces will "inherit" external-lib-dep version from the root workspace.