lerna
Overview
lerna
is a multi-package management tool that optimizes the workflow of code repositories that manage multi-packages, such as git and npm/yarn.
When developing a large project, the entire project is often split into multiple code repositories for independent versioning of the software package management, which is very useful for code sharing.
For example, the open source project babel
, the entire project was split into multiple software packages such as @babel/core
, @babel/parser
, @babel/traverse
.
But this can also make it cumbersome and difficult to track if some changes span multiple repositories. lerna
can help optimize dependencies, version management, workflows, etc. on multiple repositories.
Install
lerna can be installed globally or in a project (the following content uses the method of installing it in the project)
# npm
npm install lerna
# yarn
yarn add lerna
Simple Getting Started
Create a project and use lerna for project environment initialization
mkdir lerna-demo && cd $_
yarn init -y
yarn add lerna
npx lerna init
You will get a project folder containing the following:
lerna-demo
packages/
lerna.json
package.json
Among them, the packages/
directory is used to store all software packages. lerna.json
is the lerna configuration file.
Configuration instructions
// lerna.json
{
"useWorkspaces": true,
"npmClient": "npm", // npm | yarn
"packages": ["packages/*"],
"version": "0.0.0",
"command": {
"bootstrap": {
// more...
}
// more
}
}
npmClient
: Set the currently used package manager, the default is npm, which can be set to yarn;version
: package version number, named according to the semver version number specification;packages
: The directory where the software package is located, you can use golb to match patterns;useWorkspaces
: Use workspaces, this option can be used better with yarn;command
: Configure the various commands of lerna.
Command line instructions
lerna init
Initialize a lerna project, by default, packages/ and lerna.json will be created in the directory.
--independent
: Use the subcontracted independent version management mode, and each software package uses an independent version number.
lerna create pkgName [location]
Create a new subpackage in the project and set the package name by pkgName. The location defines the directory where the package is located, and the default is the first element of the packages configuration.
lerna add <package>[@version] [-dev] [-exact] [-peer]
Similar to yarn add
or npm install
, add dependency packages to dependency in a lerna repo.
--dev
: Indicates adding packages to devDependencies--exact
: Add a version of the package (such as 1.0.1), instead of a version-wide package (^1.0.1)--peer
: Add a pre-dependency package.
lerna bootstrap
Install the dependency library for all packages in the current lerna repo and link all same-domain dependencies.
lerna run <script>
Execute script commands in all packages in the current lerna repo.
packages/
package1/
package2/
lerna run build # is equivalent to executing npm run build in package1 and package2
- --scope Filter packages that meet the criteria
lerna run build --scope test component
- --stream Use registration as prefix to cross-out the console information flow of all packages.
lerna run build --stream
- --parallel Similar to stream.
lerna run build --parallel
lerna clean
Delete node_modules for all packages