pmwarden
v2.0.0
Published
Enforce a specific package manager in Node.js projects with clear mismatch errors.
Maintainers
Readme
pmwarden
pmwarden is a lightweight package-manager enforcement tool for Node.js projects. It helps teams and developers ensure that a project is executed with the intended package manager, reducing inconsistencies between local development, CI pipelines, and shared environments.
Supported managers include:
- npm
- pnpm
- yarn
- bun
- cnpm
Usage
Add the following preinstall script to your project package.json
{
// package.json
"scripts": {
"preinstall": "npx pmwarden pnpm"
}
}If the active environment resolves to a different package manager, pmwarden will fail fast and report the mismatch.
API reference
pmwarden exposes a small set of functions that can be imported directly in your own tooling or tests.
getPackageManager(userAgent?)
Parses the package manager name from an npm-style user agent string and returns it as a lowercase string.
import { getPackageManager } from 'pmwarden';
const manager = getPackageManager('pnpm/9.0.0 npm/10.8.2 node/v20.0.0 darwin arm64');
// => 'pnpm'If no user agent is provided, it uses the current npm environment value.
isValidPackageManager(manager)
Checks whether the provided value is one of the supported package managers.
import { isValidPackageManager } from 'pmwarden';
isValidPackageManager('pnpm'); // true
isValidPackageManager('yarn'); // true
isValidPackageManager('unknown'); // falsedetect(argv?, env?)
Validates the requested package manager against the detected one and prints a helpful error message when they do not match. It returns an exit status code:
0when the package manager matches1when a mismatch is detected2when the input is missing or invalid
import { detect } from 'pmwarden';
const exitCode = detect(['pnpm'], process.env);This is the function used by the CLI entry point.
License
This project is licensed under the Apache License 2.0. See the LICENSE file for details.
