Search code examples
monorepolernayarn-workspacesyarnpkg-v2

How to keep workspace:* version specifications when running 'lerna version'


In a given JS monorepo, sub-packages that depend on each other declare this dependency in their package.json files as

  "@foo/bar": "workspace:*",
  "@foo/qux": "workspace:*",
  ...

However, when running lerna version 1.2.3, Lerna rewrites these version specifications from workspace:* to workspace:^1.2.3.


Question: Is there a way to prevent Lerna from doing this, instead keeping workspace:*, which is still valid?


Solution

  • The original Lerna does not currently work properly with workspace: protocol (it will throw some errors and overwrite any workspace:), there was this PR #2450 that was created to bring support for this feature but as of today it has yet to be merged and it is actually very incomplete. They might bring support for this feature in the future but I wouldn't bet on it in the short term, I would assume that they will eventually do support it but when would be a huge guess (it's not even on their new roadmap).

    Alternative

    However Lerna-Lite does support it (since few months already) as mentioned in the readme (also worth knowing that there were couple of issues that came up at first, but they were all resolved and now the feature is pretty stable) and even the Lerna-Lite project itself was recently migrated to pnpm which also uses the workspace: protocol as well, this makes it easier to test its own code (dog fooding) and also to make sure everything is running smoothly. More info can be found in the version#workspace-protocol Wiki. The feature is already used by many users and it will also work just the same with Yarn 3+ (Berry that is).

    Last thing to note is that I'm the creator of the Lerna-Lite forked project, it is a smaller alternative fork that was created when Lerna was largely unmaintained (for nearly 2 years) and it was created couple months before Nrwl eventually brought back Lerna to life. The main reason I created the fork was to update all of its dependencies and also to make it more modular (install only what you need) and also bring extra features like the workspace: protocol. I now also keep track of all new PRs from the original Lerna and replicate most PRs (when relevant) to keep both projects in sync.

    EDIT

    Lerna now also supports workspace: as well in v6.x and higher, they added support for this feature about 6 months later than Lerna-Lite. So you can now use either one to get this feature.