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
or package-lock.json
in current working directory, it will skip other opeations and directly resolves yarn
or npm
- When there's no lockfile was found, it checks if
yarn
command is install. If so it resolves yarn
otherwise npm
Install
yarn add detect-package-manager
Usage
const detectPackageManager = require('detect-package-manager')
detectPackageManager()
.then(pm => {
console.log(pm)
})
API
detectPackageManager([opts])
Return: Promise<pm>
It returns a Promise resolving the name of package manager, could be either npm
or yarn
.
opts
cwd
Type: string
Default: .
The directory to look up yarn.lock
or package-lock.json
.
detectPackageManager.npmVersion()
Return: Promise<version>
It returns a Promise resolving the version of npm.
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