What is detect-package-manager?
The detect-package-manager npm package is designed to help developers identify which package manager is being used in a project. This can be particularly useful in scripts or tools that need to adapt their behavior based on the package manager in use.
What are detect-package-manager's main functionalities?
Detecting the package manager
This feature allows you to detect which package manager (e.g., npm, yarn, pnpm) is being used in the current project. The code sample demonstrates how to use the detect-package-manager package to identify the package manager and log its name.
const detectPM = require('detect-package-manager');
(async () => {
const packageManager = await detectPM();
console.log(`The package manager in use is: ${packageManager.name}`);
})();
Detecting the package manager with options
This feature allows you to specify a custom directory to detect the package manager. The code sample shows how to pass an options object with a 'cwd' property to detect the package manager in a specific directory.
const detectPM = require('detect-package-manager');
(async () => {
const packageManager = await detectPM({ cwd: '/path/to/project' });
console.log(`The package manager in use at the specified path is: ${packageManager.name}`);
})();
Other packages similar to detect-package-manager
which-pm
The which-pm package is another tool for detecting the package manager being used in a project. It provides similar functionality to detect-package-manager but focuses on simplicity and ease of use. It can be used to determine if npm, yarn, or pnpm is being used.
pkg-install
The pkg-install package is a utility that not only detects the package manager but also provides a unified API for installing dependencies. It abstracts away the differences between npm, yarn, and pnpm, making it easier to write cross-package-manager scripts.
detect-package-manager
How does this work?
- When there's
yarn.lock
, package-lock.json
, or pnpm-lock.yaml
in current working directory, it will skip other operations and directly resolve yarn
, npm
, or pnpm
. - When there's no lockfile found, it checks if
yarn
and pnpm
command exists. If so, it resolves yarn
or pnpm
otherwise npm
. - Results are cached.
Install
yarn add detect-package-manager
Usage
const { detect } = require('detect-package-manager')
detect()
.then(pm => {
console.log(pm)
})
API
detect([opts])
- Arguments:
opts.cwd
: string
Optional, defaults to .
, the directory to look up yarn.lock
, package-lock.json
, or pnpm-lock.yaml
.
- Returns:
Promise<PM>
It returns a Promise resolving the name of package manager, could be npm
, yarn
, or pnpm
.
getNpmVersion([pm])
- Arguments:
pm
: string
Optional, defaults to npm
, could be npm
, yarn
, or pnpm
- Returns:
Promise<string>
It returns a Promise resolving the version of npm or the package manager you specified.
clearCache()
Clear cache.
Contributing
- Fork it!
- Create your feature branch:
git checkout -b my-new-feature
- Commit your changes:
git commit -am 'Add some feature'
- Push to the branch:
git push origin my-new-feature
- Submit a pull request :D
Author
detect-package-manager © EGOIST, Released under the MIT License.
Authored and maintained by EGOIST with help from contributors (list).
github.com/egoist · GitHub @EGOIST · Twitter @_egoistlily