javascriptnpmlernapnpmpnpm-workspace

Moving from lerna to pnpm


I am currently migrating our project from Lerna to PNPM and we have a script we run I have posted it below

"postinstall": "npm run bootstrap"
"bootstrap": "lerna bootstrap --hoist",
"clean": "lerna clean -y",

is there a PNPM equivalent of Lerna bootstrap command and Lerna clean command?


Solution

  • You don't need any Lerna bootstrap equivalent, just get started with pnpm workspace is easy enough, you just need to add a pnpm-workspace.yaml file in your project root and add the location of your packages (which is typically packages/). As mentioned on pnpm documentation:

    A workspace must have a pnpm-workspace.yaml file in its root. A workspace also may have an .npmrc in its root.

    Hoisting is not recommended with pnpm and is disabled by default, but if you really want to hoist then you can add shamefully-hoist into .npmrc, see pnpm doc - shamefully-hoist

    By default, pnpm creates a semistrict node_modules, meaning dependencies have access to undeclared dependencies but modules outside of node_modules do not. With this layout, most of the packages in the ecosystem work with no issues. However, if some tooling only works when the hoisted dependencies are in the root of node_modules, you can set this to true to hoist them for you.

    I'm not sure about lerna clean equivalent but to remove a dependency from node module, you can use pnpm remove --recursive as per pnpm doc - remove

    When used inside a workspace (with --recursive), removes a dependency (or dependencies) from every workspace package.

    When used not inside a workspace, removes a dependency (or dependencies) from every package found in subdirectories.

    You can see an example of all of that in Lerna-Lite pnpm-workspace.yaml and I would suggest you to take a look at Lerna-Lite and maybe not give up Lerna entirely, I created Lerna-Lite fork when Lerna was no longer maintained (it is now) and the big difference is that Lerna-Lite only has a subset of the original Lerna commands (all the Lerna commands you mentioned aren't in Lerna-Lite because all package managers now handle these better than Lerna/Lerna-Lite does), the other distinction is that it's much smaller since I also made a few commands as optional packages and only kept version and publish in the core CLI, so it's much smaller when version and publish is all you need. I also don't like the new Lerna changes and the new direction that Lerna is now heading to (it's becoming more & more another Nrwl/Nx product, because Lerna v6 is now requiring Nx behind the scene, and that is not the case with Lerna-Lite, Nx can be installed with Lerna-Lite but it's totally optional and that's the way it should and will always be in Lerna-Lite).