npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

package-manager-manager

v0.2.0

Published

Utilities for managing package managers.

Downloads

297,426

Readme



package-manager-manager is a library aimed at providing information regarding the package manager currently being used in a given project.

It can be used in CLIs or similar projects which may at some point need to know and adapt their behavior based on the package manager currently used by the developer (e.g. A project scaffolding tool, a bundling tool, etc...).

Usage

To use the library first install it in your project, via:

npm i package-manager-manager

(or your package manager's equivalent)

Then simply import and use the getPackageManager() function to get an object containing all the information you need regarding the package manager currently being used:

const packageManager = await getPackageManager();

console.log(packageManager.name);
// logs 'npm', 'yarn', 'pnpm' or 'bun'

console.log(packageManager.version);
// logs the version of the package manager e.g. '8.11.0'

Note This library comes with properly defined and documented typescript types, meaning that once you obtain the PackageManager object you will be able to easily see what's available on it and get all necessary details directly in your IDE

API

getPackageInfo

packageManager.getPackageInfo allows you to get the information regarding a locally installed package that your client application is using, it can for example be used to make sure your user's application has a certain dependency or to gather and display the package version of such dependency.

For example:

const zodPackage = await packageManager.getPackageInfo('zod');
if (zodPackage) {
	console.log(`starting validation using zod (version: ${zodPackage.version}`);
} else {
	throw new Error('Error: zod is not installed');
}

Note This method only returns the information of a locally installed package, or null in case the package is not installed, it does not return information of packages not locally installed (the API could be extended in the future to also include such use case)

getRunScript

packageManager.getRunScript let's you create a command that can be used to run a script defined in the package.json file.

For example:

const buildStr = packageManager.getRunScript('build', {
	args: ['./dist', '--verbose'],
});

console.log(`To build your application run: ${buildStr}`);

If you need more fine grained control over the command you can use its packageManager.getRunScriptStruct alternative to obtain an object representing the command.

For example:

import { spawn } from 'child_process';

const buildCmd = packageManager.getRunScriptStruct('build', {
	args: ['./dist', '--verbose'],
});

// run the command for the user
spawn(buildCmd.cmd, buildCmd.cmdArgs);

getRunExec

packageManager.getRunExec let's you create a command that can be used to execute a command from a target package (which may or may not be locally installed).

For example:

const eslintStr = packageManager.getRunExec('eslint', {
	args: ['./src', '--quiet'],
});

console.log(`To run eslint on your application run: ${eslintStr}`);

If you need more fine grained control over the command you can use its packageManager.getRunExecStruct alternative to obtain an object representing the command.

For example:

import { spawn } from 'child_process';

const eslintCmd = packageManager.getRunExec('eslint', {
	args: ['./src', '--quiet'],
});

// run the command for the user
spawn(eslintCmd.cmd, eslintCmd.cmdArgs);