Lerna

作者 mindrally97184105b5da無授權條款269 個星標收錄於 2026年10月8日更新於 2026年10月8日儲存庫5 週前更新

Best practices for Lerna monorepo management, versioning, and publishing

AI 產生的概覽

提供在 Lerna monorepo 中管理、版號控制與發佈 JavaScript/TypeScript 套件的最佳實務指引。

功能
此技能提供使用 Lerna monorepo 的最佳實務說明,涵蓋專案結構、lerna.json 設定以及 workspaces 整合。內容說明任務執行、版號控管流程、發佈、變更偵測、慣例式提交、相依性管理與 CI/CD 整合。產出的是指引與設定範例,而非檔案或指令碼。
適用情境
適用於建置或維護 Lerna monorepo,或需要對多個 JavaScript/TypeScript 套件進行版號控管與發佈時。也適合將 Lerna 接入 CI,或採用慣例式提交以自動發佈。
執行需求
未包含指令碼,僅為說明性指引。套用這些指引需要 Node.js 環境、Lerna,以及 npm、yarn 或 pnpm 等套件管理工具,發佈時還需要 npm 登錄庫憑證。

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:
    • packages/ - All package directories (default)
    • 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 lerna.json at the root:

json
{  "$schema": "https://json.schemastore.org/lerna.json",  "version": "independent",  "npmClient": "npm",  "packages": ["packages/*"],  "useWorkspaces": true}
  • Choose versioning mode:
    • "version": "independent" - Each package versioned separately
    • "version": "1.0.0" - Fixed/locked mode, all packages same version
  • Enable workspaces integration with useWorkspaces: true

Workspaces Integration

Configure npm/yarn/pnpm workspaces in root package.json:

json
{  "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:
    • lerna run build - Run build in all packages
    • lerna run test --scope=@org/package - Run in specific package
    • lerna run lint --since main - Run only in changed packages
  • Use --stream for real-time output
  • Use --parallel for concurrent execution

Versioning Workflow

  • Update versions with lerna version:
    • lerna version patch - Bump patch version
    • lerna version minor - Bump minor version
    • lerna version major - Bump major version
    • lerna version - Interactive version selection
  • Lerna automatically:
    • Updates package.json versions
    • Updates internal dependency versions
    • Creates git tags
    • Pushes to remote

Publishing Packages

  • Publish with lerna publish:
    • lerna publish - Publish packages changed since last release
    • lerna publish from-git - Publish packages tagged in git
    • lerna publish from-package - Publish packages with unpublished versions
  • Configure npm registry in .npmrc or lerna.json
  • Use --dist-tag for pre-release versions

Change Detection

  • Use --since flag for changed packages:
    • lerna run test --since main
    • lerna changed - List packages changed since last tag
    • lerna diff - Show diff since last release
  • 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:
    • fix: - Patch version
    • feat: - Minor version
    • BREAKING CHANGE: - Major version
  • 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 lerna run with --since for efficient CI
  • Publish from CI with proper npm authentication
  • Use --yes flag for non-interactive publishing

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

來源與署名

來源:mindrally/skills位於lerna提交9718410

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架