2

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?

Royi Freifeld
  • 629
  • 2
  • 11
  • 31

1 Answers1

2

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.

Viktor Vlasenko
  • 2,332
  • 14
  • 16
  • Awesome! thnx! what version should I set in the peer dep section ? – Royi Freifeld Oct 19 '20 at 18:31
  • You can set `*` as a version in peer dep section, if you want to pick any version provided by root `package.json` – Viktor Vlasenko Oct 19 '20 at 19:14
  • What if the monorepo holds all the standard backend libs for the company? I think that if I set one of the libs as a dep in a service, I'll get a * peer dependency requirement – Royi Freifeld Oct 20 '20 at 12:19
  • Sorry, I can only base my answer on information that was initially present in your question. Where the information about standard backend libs can be found in your question? – Viktor Vlasenko Oct 20 '20 at 15:25