Skip to content

getPackageManager

Determines which package manager owns the project’s dependency tree.

Every signal is collected before any of them is believed, because a repo carrying two lockfiles used to resolve to whichever sat earlier in a fixed list — silently reassigning every package script and every tool invocation to a manager that never installed the tree.

The order is:

  1. The packageManager field of package.json. It is a deliberate declaration by the repo’s author rather than an artifact left behind by whatever ran last, so it outranks every lockfile — and it is the only signal that helps at all before the first install, when no lockfile exists yet. 2. The sole lockfile, when exactly one manager claims the tree. 3. Among several lockfiles, the manager that launched us (npm_config_user_agent), when it owns one of them — a live signal beats a file that may have been abandoned mid-migration. Otherwise the documented fallback order in LOCKFILES decides. Note this is deliberately NOT “the most recently modified lockfile”: a stray lockfile is typically the newest file, so mtime picks precisely the wrong one. 4. npm_config_user_agent on its own, when there is no lockfile. 5. PackageManager.Npm.

Whenever more than one manager claims the tree, the disagreement is reported once per project through warnAboutSeveralClaimants — the resolution stays deterministic, but it stops being silent.

Import:

import { getPackageManager } from 'obsidian-dev-utils/script-utils/package-manager';

Signature:

function getPackageManager(cwd: string | undefined): PackageManager

Parameters:

ParameterTypeDescription
cwdstring | undefinedThe current working folder to resolve from.

Returns: PackageManager — The detected package manager, or PackageManager.Npm when nothing indicates otherwise.