CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-lerna

Lerna is a fast, modern build system for managing and publishing multiple JavaScript/TypeScript packages from the same repository

Pending
Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

SecuritybySnyk

Pending

The risk profile of this skill

Overview
Eval results
Files

change-detection.mddocs/

Change Detection

Detecting which packages have changed since a given reference for selective operations.

Capabilities

Changed Command

List packages that have changed since the last release or specified reference.

# List changed packages since last release
lerna changed
lerna updated  # Alias for 'changed'

# List changed packages since specific ref
lerna changed --since HEAD~1
lerna changed --since v1.0.0
lerna changed --since origin/main

# Include dependencies of changed packages
lerna changed --include-dependencies

# Include dependents of changed packages  
lerna changed --include-dependents

# Include packages from merged tags
lerna changed --include-merged-tags

# Output formats
lerna changed --json          # JSON array
lerna changed --ndjson        # Newline-delimited JSON
lerna changed --parseable     # Parseable format
lerna changed --long          # Extended information

# Filtering
lerna changed --scope="@myorg/*"      # Include only matching packages
lerna changed --ignore="*-test"       # Exclude matching packages
lerna changed --no-private            # Exclude private packages
lerna changed --all                   # Include all packages

Diff Command

Show git diff for packages that have changed since the last release.

# Show diff for all changed packages
lerna diff

# Show diff for specific package
lerna diff package-name

# Show diff since specific reference  
lerna diff --since HEAD~1
lerna diff --since v1.0.0

# Show diff for specific package since reference
lerna diff package-name --since origin/main

Diff command:

  • Runs git diff on packages that have changed
  • Similar to lerna changed but shows actual file differences
  • Supports same filtering options as lerna changed
  • Shows changes since last release tag by default
  • Can target specific packages or all changed packages

Change Detection Logic

Lerna detects changes using git diff between:

  • Last release tag (default): Most recent git tag
  • Specific reference: Commit, branch, or tag provided via --since

Detection includes:

  • Modified files in package directories
  • Added/removed files
  • Changes to package.json dependencies
  • Git submodule changes affecting packages

Since Reference Options

# Commit references
lerna changed --since HEAD~3
lerna changed --since abc1234

# Branch references  
lerna changed --since origin/main
lerna changed --since feature/new-api

# Tag references
lerna changed --since v1.0.0
lerna changed --since latest

# Time-based references
lerna changed --since "2 weeks ago"
lerna changed --since "2023-01-01"

Advanced Change Detection

Dependency Inclusion

Include packages based on their relationships:

# Include dependencies (packages that changed packages depend on)
lerna changed --include-dependencies

# Include dependents (packages that depend on changed packages) 
lerna changed --include-dependents

# Include both dependencies and dependents
lerna changed --include-dependencies --include-dependents

Example scenario:

  • Package A depends on Package B
  • Package B changed
  • --include-dependents includes Package A (depends on changed B)
  • --include-dependencies includes Package B's dependencies

Merged Tag Handling

# Include packages affected by merged pull requests
lerna changed --include-merged-tags

# Useful for CI/CD after merging features
lerna changed --since origin/main --include-merged-tags

This option is particularly useful when:

  • Features are developed in branches with their own version tags
  • Tags are merged rather than rebased
  • You need to detect all affected packages across merged branches

Force Detection Override

# Force include all packages (ignore change detection)
lerna changed --force-publish

# Force include specific packages
lerna changed --force-publish package-a,package-b

# Force include packages matching pattern
lerna changed --force-publish "@myorg/*"

Output Formats

JSON Output

# JSON array format
lerna changed --json
# ["package-a", "package-b", "package-c"]

# Newline-delimited JSON
lerna changed --ndjson  
# "package-a"
# "package-b"
# "package-c"

Extended Information

# Show version and location
lerna changed --long
# package-a v1.2.3 packages/package-a
# package-b v0.5.1 packages/package-b

# Parseable output (machine-readable)
lerna changed --parseable
# /path/to/workspace/packages/package-a:package-a:1.2.3
# /path/to/workspace/packages/package-b:package-b:0.5.1

Integration with Other Commands

Script Execution

# Run tests only on changed packages
lerna run test --since HEAD~1

# Build changed packages and their dependents
lerna run build --since v1.0.0 --include-dependents

Publishing

# Publish only changed packages
lerna publish --since HEAD~1

# Version only changed packages
lerna version --since v1.0.0

Package Operations

# Execute command only in changed packages
lerna exec --since HEAD~1 -- rm -rf node_modules

# List only changed packages
lerna list --since origin/main

Configuration

Default Since Reference

Configure default reference in lerna.json:

{
  "command": {
    "changed": {
      "since": "origin/main",
      "includeDependents": true
    }
  }
}

Ignore Changes

Ignore specific files/directories for change detection:

{
  "command": {
    "changed": {
      "ignoreChanges": [
        "**/*.md",
        "**/test/**",
        "**/*.test.js"
      ]
    }
  }
}

Ignore patterns:

  • **/*.md - Ignore all markdown files
  • **/test/** - Ignore test directories
  • **/__tests__/** - Ignore Jest test directories
  • **/stories/** - Ignore Storybook files
  • **/*.test.{js,ts} - Ignore test files

CI/CD Integration

GitHub Actions

- name: Check for changes
  id: changes
  run: |
    if lerna changed --since origin/main; then
      echo "changed=true" >> $GITHUB_OUTPUT
    else
      echo "changed=false" >> $GITHUB_OUTPUT
    fi

- name: Run tests on changed packages
  if: steps.changes.outputs.changed == 'true'
  run: lerna run test --since origin/main

Conditional Pipeline Steps

# Exit with code 1 if no changes (useful for CI)
lerna changed --since origin/main || exit 1

# Store changed packages for later use
CHANGED_PACKAGES=$(lerna changed --since HEAD~1 --json)
echo "Changed packages: $CHANGED_PACKAGES"

Troubleshooting

No Changes Detected

Common causes and solutions:

# Verify git history
git log --oneline --graph

# Check for uncommitted changes
git status

# Verify package locations match lerna.json
lerna list --all

# Check ignore patterns
lerna changed --since HEAD~1 --loglevel verbose

False Positives

# Add ignore patterns for non-functional changes
lerna changed --ignore-changes "**/*.md" --ignore-changes "**/test/**"

# Use specific since reference
lerna changed --since $(git describe --tags --abbrev=0)

docs

change-detection.md

configuration.md

index.md

package-management.md

package-operations.md

publishing.md

script-execution.md

version-management.md

tile.json