Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common dependency lib version with yarn workspaces

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?

like image 772
Royi Freifeld Avatar asked Oct 20 '25 05:10

Royi Freifeld


1 Answers

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.

like image 144
Viktor Vlasenko Avatar answered Oct 22 '25 05:10

Viktor Vlasenko