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

package-operations.mddocs/

Package Operations

Executing arbitrary commands across packages and managing package relationships.

Capabilities

Execute Commands

Run arbitrary commands across packages with flexible filtering and execution options.

# Execute command in all packages
lerna exec -- <command>

# Execute specific commands
lerna exec -- rm -rf node_modules
lerna exec -- npm audit fix
lerna exec -- git status
lerna exec -- ls -la

# Execute with package filtering
lerna exec --scope="@myorg/*" -- npm run build
lerna exec --ignore="*-test" -- npm install
lerna exec --since HEAD~1 -- npm test

# Execute in parallel
lerna exec --parallel -- npm run lint

# Execute with streaming output
lerna exec --stream -- npm run start

# Execute in specific directory
lerna exec --scope=package-name -- npm run dev

# Pass environment variables
lerna exec -- env NODE_ENV=production npm run build

Exec Command Options

# Execution control
--parallel          # Execute in parallel across packages
--stream            # Stream output immediately
--concurrency <n>   # Limit concurrent processes
--bail              # Stop on first command failure (default)
--no-bail           # Continue despite command failures
--no-prefix         # Do not prefix streaming output
--profile           # Profile command executions and output performance data
--profile-location  # Output performance profile to custom location

# Package filtering
--scope <glob>      # Include packages matching glob
--ignore <glob>     # Exclude packages matching glob
--since <ref>       # Include packages changed since ref
--include-dependencies    # Include dependencies
--include-dependents     # Include dependents
--no-private        # Exclude private packages

# Output control
--silent            # Suppress command output
--loglevel <level>  # Control log verbosity

Clean Operation

Remove node_modules directories from all packages.

# Remove node_modules from all packages
lerna clean

# Clean with confirmation
lerna clean --yes

# Clean specific packages
lerna clean --scope="@myorg/*"
lerna clean --ignore="core-*"

# Clean changed packages only
lerna clean --since HEAD~1

Clean operation:

  • Removes node_modules directories from all packages
  • Preserves root node_modules (workspace dependencies)
  • Prompts for confirmation unless --yes is used
  • Respects package filtering options

Import Package

Import an external package repository into the monorepo with full commit history.

# Import external repository
lerna import <path-to-external-repository>

# Import with options
lerna import /path/to/external-repo --flatten
lerna import /path/to/external-repo --max-buffer=1048576

# Import examples
lerna import ../my-existing-package
lerna import ~/projects/standalone-library

Import operation:

  • Imports entire git history from external repository
  • Preserves original commit authors, dates, and messages
  • Applies commits to current branch with package prefix
  • Moves files to packages/<directory-name>/ structure
  • Requires clean working directory before import
  • External repository remains unchanged

Import options:

  • --flatten - Import history in "flat" mode (single level)
  • --max-buffer=<size> - Increase buffer size for large repositories
  • --dest=<directory> - Specify destination directory name

Advanced Operations

Environment-Specific Commands

# Development environment setup
lerna exec -- npm run setup:dev

# Production builds
lerna exec --parallel -- npm run build:prod

# Environment-specific installs
lerna exec -- npm install --production

File System Operations

# Remove build artifacts
lerna exec -- rm -rf dist/ build/ lib/

# Copy configuration files
lerna exec -- cp ../shared/.eslintrc.js .

# Create directories
lerna exec -- mkdir -p dist/assets

# Archive packages
lerna exec -- tar -czf "../archive/${PWD##*/}.tar.gz" .

Package Maintenance

# Update dependencies
lerna exec -- npm update

# Audit and fix vulnerabilities
lerna exec -- npm audit fix

# Clean and reinstall
lerna clean --yes && npm install

# Update package metadata
lerna exec -- npm pkg set engines.node=">=18"

Git Operations

# Check git status across packages
lerna exec -- git status --porcelain

# Add git hooks
lerna exec -- cp ../shared/pre-commit .git/hooks/

# Reset git state
lerna exec -- git reset --hard HEAD

# Apply git patches
lerna exec -- git apply ../patches/${PWD##*/}.patch

Command Context

Available Variables

Commands executed via lerna exec have access to:

  • $LERNA_PACKAGE_NAME - Current package name
  • $LERNA_ROOT_PATH - Path to workspace root
  • $PWD - Current package directory
  • All standard environment variables
# Use package name in command
lerna exec -- echo "Building $LERNA_PACKAGE_NAME"

# Reference workspace root
lerna exec -- cp "$LERNA_ROOT_PATH/shared/config.js" ./

# Package-specific operations
lerna exec -- mkdir -p "$LERNA_ROOT_PATH/dist/$LERNA_PACKAGE_NAME"

Working Directory

Commands execute in each package's root directory:

# Current directory is package root
lerna exec -- pwd
# /workspace/packages/package-a
# /workspace/packages/package-b

# Access package.json
lerna exec -- cat package.json

# Access source files
lerna exec -- ls src/

Filtering and Selection

Scope-Based Filtering

# Include packages by pattern
lerna exec --scope="@myorg/*" -- npm run build
lerna exec --scope="*-api" -- npm test
lerna exec --scope="frontend-*" -- npm run lint

# Exclude packages by pattern
lerna exec --ignore="*-test" -- npm run build  
lerna exec --ignore="@internal/*" -- npm publish

# Multiple patterns
lerna exec --scope="@myorg/*" --ignore="*-test" -- npm run validate

Change-Based Filtering

# Execute only in changed packages
lerna exec --since HEAD~1 -- npm run build

# Include dependencies of changed packages
lerna exec --since v1.0.0 --include-dependencies -- npm test

# Include dependents of changed packages
lerna exec --since origin/main --include-dependents -- npm run build

Property-Based Filtering

# Execute only in public packages
lerna exec --no-private -- npm publish

# Execute only in private packages
lerna exec --private -- npm run internal-check

Performance and Parallelization

Parallel Execution

# Maximum parallelism
lerna exec --parallel -- npm run lint

# Controlled concurrency
lerna exec --concurrency 4 -- npm test

# Stream output for monitoring
lerna exec --parallel --stream -- npm run dev

Use parallel execution for:

  • Independent operations (lint, test, format)
  • Long-running processes (dev servers)
  • Operations that don't depend on other packages

Use sequential execution for:

  • Build operations with dependencies
  • Operations that modify shared resources
  • Commands that require specific ordering

Error Handling

# Stop on first failure (default)
lerna exec --bail -- npm run build

# Continue despite failures
lerna exec --no-bail -- npm test

# Combine with logging for analysis
lerna exec --no-bail --loglevel=error -- npm audit

Integration Patterns

CI/CD Pipeline Integration

# Pre-deployment checks
lerna exec --parallel -- npm run lint
lerna exec --since HEAD~1 -- npm test
lerna exec --no-private -- npm run build

# Post-deployment cleanup
lerna exec -- npm prune --production

Development Workflow

# Setup new development environment
lerna clean --yes
npm install
lerna exec -- npm run setup

# Daily development routine
lerna exec --since HEAD~1 --include-dependents -- npm run build
lerna exec --parallel -- npm run test:watch

Maintenance Tasks

# Security updates
lerna exec -- npm audit fix --force

# Dependency updates
lerna exec -- npm update
lerna exec -- npm outdated

# Code formatting
lerna exec --parallel -- npm run format
lerna exec --parallel -- npm run lint:fix

docs

change-detection.md

configuration.md

index.md

package-management.md

package-operations.md

publishing.md

script-execution.md

version-management.md

tile.json