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

script-execution.mddocs/

Script Execution

Running npm scripts across packages with dependency-aware execution and caching.

Capabilities

Run Scripts

Execute npm scripts across multiple packages with intelligent dependency handling.

# Run script in all packages that have it
lerna run <script>

# Run specific script examples
lerna run test
lerna run build  
lerna run lint
lerna run start

# Run script in specific packages
lerna run test --scope=package-name
lerna run build --scope="@myorg/*"

# Run script in changed packages only
lerna run test --since HEAD~1
lerna run build --since v1.0.0

# Run script with parallel execution
lerna run test --parallel

# Run script with streaming output
lerna run test --stream

# Run script in topological order (dependencies first)
lerna run build --sort

# Ignore script failures and continue
lerna run test --no-bail

# Include dependencies/dependents
lerna run build --include-dependencies
lerna run test --include-dependents

# Pass arguments to scripts
lerna run test -- --coverage
lerna run build -- --minify --sourcemap

# Run multiple scripts concurrently (comma-separated)
lerna run "lint,test,build"
lerna run "test,lint" --parallel

Script Execution Options

# Execution control
--parallel          # Run scripts in parallel (ignores concurrency)
--stream            # Stream output immediately (implies --parallel)
--no-sort           # Don't run in topological order
--concurrency <n>   # Maximum number of concurrent processes
--bail              # Stop on first script failure (default)
--no-bail           # Continue despite script failures

# Output control  
--silent            # Suppress script output
--loglevel <level>  # Set log level (silent, error, warn, notice, http, timing, info, verbose, silly)
--no-prefix         # Do not prefix streaming output
--npm-client <cmd>  # Executable used to run scripts (npm, yarn, pnpm, etc.)

# 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 of matched packages
--include-dependents     # Include dependents of matched packages
--include-merged-tags    # Include packages from merged tags

# Nx integration (when useNx: true)
--skip-nx-cache     # Skip Nx caching
--nx-bail           # Stop on first Nx task failure
--nx-ignore-cycles  # Ignore dependency cycles in Nx

Parallel Execution

# Run scripts in parallel
lerna run build --parallel

# Control concurrency
lerna run test --concurrency 4

# Stream output for better feedback
lerna run start --stream

Parallel execution:

  • Ignores package dependency order
  • Runs all matching packages simultaneously
  • Best for independent operations (test, lint)
  • Use --stream for real-time output

Sequential execution (default):

  • Respects package dependency order
  • Runs packages topologically
  • Best for build operations where dependencies matter

Nx Integration

When useNx: true in lerna.json, script execution uses Nx task runner:

# Nx-powered script execution with caching
lerna run build

# Skip Nx cache
lerna run build --skip-nx-cache

# Nx task configuration via nx.json
lerna run build --with-deps  # Uses Nx dependency detection

Nx benefits:

  • Intelligent caching based on file changes
  • Distributed task execution
  • Better performance for large workspaces
  • Advanced dependency detection

Script Configuration

Package Scripts

Define scripts in package.json:

{
  "scripts": {
    "build": "tsc",
    "test": "jest",
    "lint": "eslint src/",
    "start": "node dist/index.js",
    "clean": "rimraf dist/"
  }
}

Workspace Scripts

Run scripts across the workspace:

# Run build in all packages that have it
lerna run build

# Packages without the script are skipped automatically
lerna run nonexistent-script  # Runs successfully, skips packages

Script Dependencies

For dependent builds, use topological ordering:

# Build dependencies first, then dependents
lerna run build --sort

# Equivalent: (default behavior unless --parallel)
lerna run build

Advanced Usage

Conditional Execution

# Run only in packages with changes
lerna run build --since origin/main

# Run in packages with specific dependency
lerna run integration-test --scope="*api*"

# Run excluding test packages  
lerna run build --ignore="*-test" --ignore="test-*"

Script Arguments

Pass arguments to underlying scripts:

# Pass coverage flag to test script
lerna run test -- --coverage --reporter=lcov

# Pass multiple arguments
lerna run build -- --minify --sourcemap --verbose

# Environment-specific builds
lerna run build -- --env=production --optimize

Error Handling

# Stop on first failure (default)
lerna run test --bail

# Continue despite failures
lerna run test --no-bail

# Combine with reporting
lerna run test --no-bail --stream > test-results.log

Output Management

# Suppress script output
lerna run build --silent

# Stream output for long-running processes
lerna run start --stream

# Control log levels
lerna run build --loglevel=error  # Only show errors
lerna run test --loglevel=verbose # Show detailed output

Performance Optimization

Caching with Nx

Configure nx.json for optimal caching:

{
  "targetDefaults": {
    "build": {
      "dependsOn": ["^build"],
      "cache": true,
      "outputs": ["{projectRoot}/dist"]
    },
    "test": {
      "cache": true,
      "outputs": ["{projectRoot}/coverage"]
    }
  }
}

Concurrency Control

# Optimize for CPU cores
lerna run build --concurrency $(nproc)

# Conservative concurrency for memory-intensive tasks
lerna run test --concurrency 2

# Maximum parallelism for independent tasks
lerna run lint --parallel

Selective Execution

# Only build changed packages and their dependents
lerna run build --since HEAD~1 --include-dependents

# Test only packages with changes
lerna run test --since origin/main

# Build only public packages  
lerna run build --no-private

docs

change-detection.md

configuration.md

index.md

package-management.md

package-operations.md

publishing.md

script-execution.md

version-management.md

tile.json