package-manager-detector

Detect the package manager used in a project from lock files and metadata

Library
npm
v1.8.0
168stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
67/100Good
Development Activity60
Maintenance76
Community60
Maturity44
Momentum28

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
84/100Excellent
Architecture84
Code Quality85
Innovation78
Learning Curve90

package-manager-detector is a tiny, dependency-free TypeScript library that determines which JavaScript package manager a project uses. It inspects lock files, the packageManager and devEngines fields in package.json, and installation metadata to identify npm, Yarn, pnpm, Deno, Bun, and other supported managers.

Beyond detection, it exposes a normalized set of agents and command mappings, so tools can resolve the correct install, run, add, or execute command for whichever package manager is in use. It is widely relied upon by build tools and CLIs that need to stay package-manager agnostic.

What You Get

  • A detect API that finds the active package manager by walking up the directory tree
  • A getUserAgent helper to identify the currently running package manager
  • Configurable detection strategies (lockfile, packageManager-field, devEngines-field, install-metadata)
  • Normalized agent and command mappings for install, run, add, and execute across managers
  • Zero runtime dependencies and full TypeScript types

Common Use Cases

  • Making a CLI or build tool package-manager agnostic
  • Generating the correct install/run commands in docs or scaffolding output
  • Detecting the package manager to invoke the right underlying command
  • Resolving the active manager inside monorepo tooling

Under The Hood

Architecture - The source is a small, focused set of TypeScript modules: detect.ts implements the strategy-driven detection loop (lock files, packageManager/devEngines fields, install metadata) with upward directory crawling; commands.ts and constants.ts define the agent and command mapping tables; and types.ts plus index.ts expose the typed public surface. Subpath exports (./detect, ./commands, ./constants) let consumers import only what they need.

Tech Stack - Written entirely in TypeScript with no runtime dependencies, built with unbuild into ESM output, and developed with a modern toolchain: pnpm workspaces, ESLint via @antfu/eslint-config, and Vitest for testing.

Code Quality - The test directory covers detection, commands, user-agent parsing, and public exports, with snapshot tests and fixtures for realistic project layouts. The tiny, dependency-free surface and strong typing keep the code easy to audit and reason about.

API Design - The public API is minimal and ergonomic: an async detect() with an options object for strategy ordering, plus getUserAgent() and command-resolution helpers. Subpath entry points keep imports lean, and the whole thing is designed to be embedded in other tools with near-zero configuration.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search