This package includes a bin which can be used to detect the current node package manager a node project uses and it's associated commands.
No need to install, can be run via node exec commands such as:
npx pm-detect
The above command will log the following JSON blob to stdout (if the project uses NPM):
{
"name": "npm",
"install": "npm install",
"frozen-install": "npm ci",
"global-install": "npm install -g",
"run": "npm run",
"exec": "npx"
}The structure of the output is always the same regardless of package manager (NPM, yarn, PNPM, bun). It is only the values of the object that change.
The pm-detect CLI supports the following options:
Specifies the working directory to detect the package manager in. If not provided, the current working directory is used.
npx pm-detect --working-dir /path/to/projectNote
If no package manager is detected in the working directory the cli will traverse up the directory tree until it finds one. This should prove useful in monorepos where a workspace might be passed as the current working directory.
Specifies a comma-separated list of detection strategies to use. The default order is packageJson,installState,lockFile,userAgent — the file-based strategies (packageJson, installState, lockFile) run in the order given against each directory while walking up from the working directory, and the first match wins. userAgent is a post-walk fallback: it only runs if no file-based strategy matched in any directory, regardless of where it appears in the list. Available strategies:
packageJson- Detect frompackage.json'spackageManagerfield (most authoritative; recommended)installState- Detect from metadata each manager writes during install:node_modules/.modules.yaml(pnpm, with version),node_modules/.yarn-state.ymlor.pnp.cjs(yarn berry; version recovered from.yarn/releases/yarn-*.cjs),node_modules/.yarn-integrity(yarn classic),node_modules/.package-lock.json(npm)lockFile- Detect from lock file presence (bun.lock,bun.lockb,package-lock.json,yarn.lock,pnpm-lock.yaml)userAgent- Detect from thenpm_config_user_agentenvironment variable (post-walk fallback only)
installState is preferred over lockFile by default because it reflects what is actually installed, which is more authoritative than a lockfile that may lag behind a manager switch.
# Use only package.json detection
npx pm-detect --strategies packageJson
# Use multiple strategies in a specific order
npx pm-detect --strategies packageJson,installState,lockFile,userAgentShows the help message with all available options.
npx pm-detect --helpShows the current version of the CLI tool.
npx pm-detect --version# Basic usage - detects package manager in current directory
npx pm-detect
# Detect in a specific directory
npx pm-detect --working-dir /path/to/project
# Use only lock file detection strategy
npx pm-detect --strategies lockFile
# Combine options
npx pm-detect --working-dir /path/to/project --strategies packageJson,lockFileThis project is based heavily on the following npm packages. Many thanks to their authors.
Thanks goes to these wonderful people (emoji key):
Jack Westbrook 📖 🚇 |
Tom 📖 |
This project follows the all-contributors specification. Contributions of any kind welcome!
This package is published using NPMs provenence feature that asserts a given release is tied to a specific commit.