Skip to main content
Better PM detects your position in a monorepo and automatically adjusts install behavior to prevent accidental full-monorepo installations.

How Context Detection Works

When you run pm i, Better PM analyzes your current directory to determine the monorepo context:
1

Find the nearest package.json

Better PM searches upward from your current directory using findUpward('package.json') to locate the nearest package definition.
2

Determine context type

Better PM compares the package.json location with the lock file directory to determine if you’re in:
  • A package directory: When the package.json is in a subdirectory of the lock file location
  • The root directory: When the package.json is at the same level as the lock file
3

Apply appropriate install strategy

Based on the detected context, Better PM either:
  • Auto-scopes the install to the current package and its dependencies
  • Shows a warning and requires confirmation for root-level installs

Auto-Scoping from Package Directories

When you run pm i from inside a workspace package, Better PM automatically filters the install to that package:

How Auto-Filtering Works

Better PM builds a filtered install command based on the package manager:
pnpm uses the ... suffix (e.g., @myapp/web...) which installs the package and all its dependencies.bun and npm resolve the full dependency graph of workspace packages by reading package.json files and following workspace: protocol dependencies.
The install command then executes:

Root Directory Warnings

When you run pm i at the monorepo root, Better PM protects you from accidentally installing all packages:

Safety Features

Workspace Package List

The warning displays a formatted tree of all workspace packages using the formatWorkspaceTree function, helping you understand the scope of what would be installed.

Confirmation Prompt

In interactive environments, Better PM prompts for confirmation before proceeding with a full install. The prompt can be bypassed with the -y or --sure flag.

Filter Flag Usage

You can manually specify which packages to install using the -F or --filter flag:

How Filters Override Auto-Scoping

When filters are provided, they take precedence over context detection:
The filter flag is converted to the appropriate package manager syntax:
Filters bypass the root directory warning, so use them carefully when installing multiple packages from the root.

Context Type Reference

Better PM uses two context types internally:
  • root: You’re at the monorepo root or in a directory without a package.json
  • package: You’re inside a workspace package directory
This context drives all auto-scoping and warning behavior in Better PM.