Is Lerna needed anymore with NPM 7.0.0 workspaces?
I dont really have experience with this new npm feature.
Npm/rfcs writes regarding this:
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 :)
NPM 7 is out and it supports workspaces. they will also continue to develop this domain in the upcoming releases,
With that said, lerna
comes with many more high level features than npm7
or yarn
workspaces, furthermore, yarn
declared that they will never try to replace tools like lerna
, rather they intend to implement the core logic of handling workspaces like installing children packages dependencies and packages symlink. I expect npm
to do the same.
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.
The only command as of today supported by npm7
related to workspaces is actually npm i
/ npm ci
which is not new, but it does take care of nested packages and symlink.
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.
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:
but the correct order should be:
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.