Is Lerna needed anymore with NPM workspaces?
Asked Answered
E

3

52

Is Lerna needed anymore with NPM 7.0.0 workspaces?

I don't really have experience with this new npm feature.

npm/RFCs writes:

First and foremost there’s the alternative of leaving the problem set for userland to solve, there’s already the very popular project Lerna that provides some of these features.

Also available is the alternative of supporting only the install (or bootstrap as Lerna names it) aspect of this proposal, following a less feature-rich approach but one that would still enable the basic goal of improving the user experience of managing multiple child packages but from all the feedback collected during the research phase of this RFC, this alternative is much less desirable to the community of maintainers involved.

Glad for every answer and explanation :)

Eugenieeugenio answered 19/11, 2020 at 10:19 Comment(0)
P
51

NPM has supported workspaces since version 7, for two major releases now.

With workspaces, npm i / npm ci takes care of nested packages and symlinking. npm run and npm exec can be run across individual or all workspaces:

npm run test --workspace=a --workspace="name-from-package-dot-json"
npm run test --workspaces --if-present

With that said, lerna comes with many more high level features than npm or yarn workspaces.

One great example is the command: lerna changed which gives you the list of packages that have changed since the last tagged release, this could be extremely helpful for CI/CD. you are welcome to explore the extra commands provided by lerna.

I've written an article that goes deeper into the configuration in case you want to move to a monorepo with npm7, so working without lerna is definitely an option, you will probably need to do more work on the CI/CD side compared to lerna and add some dev scripts by your self that will affect the nested packages. Also IMO lerna fits more to develop libs rather than apps.

Petrolatum answered 20/11, 2020 at 7:10 Comment(2)
As of npm 7.7, npm run and npm exec are now supported and can be used to run commands across multiple workspaces/packages.Stellular
I've updated this answer to include current npm features. I hope that's OK @jony89.Bluebell
E
16

The answer is yes, you still need Lerna or other tool to complement the features that came with npm@7 workspaces. These are the things that are not handled by npm@7 workspaces (as of writing of this answer):

Understanding the monorepo topology

npm workspaces are aware of monorepo package topology to certain level. Workspaces for example know that package-c uses package-a and package-b as its dependencies. But there is a small thing to keep in mind:

$ npm run build --workspaces

This command will run npm run build for all the monorepo packages.

Let’s say that package-a depends on package-b and package-c depends both on package-a and package-b. The order of execution you get from running the command is depends on your workspaces array in package.json. So if you have this:

{
  "workspaces": ["package-a", "package-b", "package-c"]
}

then the build order will be:

  • package-a
  • package-b
  • package-c

but the correct order should be:

  • package-b
  • package-a
  • package-c

For things to build in the correct order, you should ensure you list them in the correct order in package.json:

{
  "workspaces": ["package-b", "package-a", "package-c"]
}

Change management

Lerna can detect changes in monorepo and provide you with a list of packages that have changed. This is handy if you only want to run tests for changed packages. npm@7 workspaces can do no such thing yet (Oct 5th, 2021).

Publishing

Lerna can manage versioning and publishing of your packages. Comes with two different strategies of managing versions: fixed and independent. It generates changelog, and publishes only changed packages to npm.

There is much more to it for sure, but these are the main things that you still need on top of npm@7 workspaces. If you use Lerna or other tools, that's up to you.

I've documented all the things I have learned while maintaining JavaScript monorepo with Lerna in an article. It describes how monorepo management processes simplified significantly after npm@7 introduction, but why we still need to use Lerna or other tools on top of it.

Englacial answered 27/9, 2021 at 8:24 Comment(2)
Plus, conventionalCommitsis pre-wired in lerna, it can fully automate the changelog generation. Plus, for more granular control, you can do versioning only and release using simple scripts which call npm publish || : (which would not exit with zero code if there's nothing to publish; and npm won't let you publish un-bumped package.json's).Rogers
I'd like to add extra info to this great answer, NPM has an open RFC to Execute workspace commands in good order which hopefully will land in the future. On the version/publish side of things I created a lib derived from Lerna in which I extracted only 2 commands (version & publish) since I wanted to keep the conventional-changelog but I didn't want all the extra stuff of Lerna, you can take a look at ws-conventional-version-roller, the lib itself is purposely created as NPM WorkspaceConfluence
A
0

From a management perspective yes. Since it can handle things like change reports.

However, if you simply want to have a monorepos and you know the dependency build order rather than relying on packages/* there's no need for it. For the most part you can simply use the following sequence to do your builds and do away with lerna and dependencies until you really need it.

yarn install --frozen-lockfile
yarn workspaces run prepare
yarn workspaces run test
Allelomorph answered 17/1, 2023 at 18:58 Comment(1)
side note, the question is in regards to NPM while you're providing an answer with Yarn which is not entirely related to the original questionConfluence

© 2022 - 2024 — McMap. All rights reserved.