DEV Community

Alex Spinov
Alex Spinov

Posted on

Lerna Has a Free API — Here's How to Use It for Monorepo Package Management

Lerna is the original JavaScript monorepo tool, now maintained by Nx. It handles versioning, publishing, and task running for multi-package repositories.

Getting Started

npx lerna init
Enter fullscreen mode Exit fullscreen mode

lerna.json Configuration

{
  "$schema": "node_modules/lerna/schemas/lerna-schema.json",
  "version": "independent",
  "npmClient": "pnpm",
  "useWorkspaces": true
}
Enter fullscreen mode Exit fullscreen mode

Running Commands Across Packages

# Run build in all packages (topological order)
lerna run build

# Run tests in parallel
lerna run test --parallel

# Run only in specific packages
lerna run build --scope=@myorg/core --scope=@myorg/utils

# Run in changed packages only
lerna run build --since=main
Enter fullscreen mode Exit fullscreen mode

Versioning

# Independent versioning — each package versioned separately
lerna version --conventional-commits

# Fixed versioning — all packages share same version
lerna version patch

# Preview changes without committing
lerna version --no-push --no-git-tag-version

# See what changed
lerna changed
lerna diff
Enter fullscreen mode Exit fullscreen mode

Publishing to npm

# Publish all changed packages
lerna publish

# Publish from Git tags
lerna publish from-git

# Publish pre-release
lerna publish --canary

# Dry run
lerna publish --no-push
Enter fullscreen mode Exit fullscreen mode

Package Listing and Filtering

# List all packages
lerna ls

# List with details
lerna ls -l

# List changed packages
lerna changed

# Show dependency graph
lerna ls --graph --all
Enter fullscreen mode Exit fullscreen mode

Task Caching (with Nx)

Since Lerna 6+, Nx powers task execution. This means free computation caching:

// nx.json
{
  "tasksRunnerOptions": {
    "default": {
      "runner": "nx/tasks-runners/default",
      "options": {
        "cacheableOperations": ["build", "test", "lint"]
      }
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

Need to extract or automate web content at scale? Check out my web scraping tools on Apify — no coding required. Or email me at spinov001@gmail.com for custom solutions.

Top comments (0)