lerna
Original:🇺🇸 English
Translated
Best practices for Lerna monorepo management, versioning, and publishing
7installs
Sourcemindrally/skills
Added on
NPX Install
npx skill4agent add mindrally/skills lernaTags
Translated version includes tags in frontmatterSKILL.md Content
View Translation Comparison →Lerna Monorepo Development
You are an expert in Lerna, the fast, modern build system for managing and publishing multiple JavaScript/TypeScript packages.
Project Structure
- Organize packages following Lerna conventions:
- - All package directories (default)
packages/ - Can customize with multiple directories in
lerna.json
- Each package should be self-contained with its own:
package.json- Source code
- Tests
- Build configuration
Lerna Configuration
Configure at the root:
lerna.jsonjson
{
"$schema": "https://json.schemastore.org/lerna.json",
"version": "independent",
"npmClient": "npm",
"packages": ["packages/*"],
"useWorkspaces": true
}- Choose versioning mode:
- - Each package versioned separately
"version": "independent" - - Fixed/locked mode, all packages same version
"version": "1.0.0"
- Enable workspaces integration with
useWorkspaces: true
Workspaces Integration
Configure npm/yarn/pnpm workspaces in root :
package.jsonjson
{
"workspaces": ["packages/*"],
"private": true
}- Let the package manager handle hoisting and linking
- Use Lerna for versioning, publishing, and running scripts
Task Execution
- Run scripts across packages:
- - Run build in all packages
lerna run build - - Run in specific package
lerna run test --scope=@org/package - - Run only in changed packages
lerna run lint --since main
- Use for real-time output
--stream - Use for concurrent execution
--parallel
Versioning Workflow
- Update versions with :
lerna version- - Bump patch version
lerna version patch - - Bump minor version
lerna version minor - - Bump major version
lerna version major - - Interactive version selection
lerna version
- Lerna automatically:
- Updates package.json versions
- Updates internal dependency versions
- Creates git tags
- Pushes to remote
Publishing Packages
- Publish with :
lerna publish- - Publish packages changed since last release
lerna publish - - Publish packages tagged in git
lerna publish from-git - - Publish packages with unpublished versions
lerna publish from-package
- Configure npm registry in or
.npmrclerna.json - Use for pre-release versions
--dist-tag
Change Detection
- Use flag for changed packages:
--sincelerna run test --since main- - List packages changed since last tag
lerna changed - - Show diff since last release
lerna diff
- Leverage affected commands in CI for efficiency
Conventional Commits
Enable conventional commits for automated versioning:
json
{
"command": {
"version": {
"conventionalCommits": true,
"message": "chore(release): publish"
}
}
}- Commits determine version bumps:
- - Patch version
fix: - - Minor version
feat: - - Major version
BREAKING CHANGE:
- Automatic changelog generation
Dependency Management
- Use internal package references:
json
{ "dependencies": { "@org/shared-utils": "^1.0.0" } } - Lerna keeps internal dependencies in sync during versioning
- Hoist common dependencies to root with workspaces
CI/CD Integration
- Install dependencies once at root level
- Use with
lerna runfor efficient CI--since - Publish from CI with proper npm authentication
- Use flag for non-interactive publishing
--yes
Best Practices
- Keep packages focused and single-purpose
- Use consistent package naming:
@org/package-name - Maintain clear dependency boundaries between packages
- Document package APIs and usage
- Use TypeScript with project references for type checking
- Implement proper testing at package and integration levels
- Consider Nx integration for advanced caching and task execution