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

get-pm

v0.0.1

Published

Detect and abstract away package managers

Downloads

6

Readme

get-pm

Detect and abstract away package managers in JS/TS codebases

Why?

Many a time, I find myself creating CLIs that require some sort of access to the user's package manager. This is a pain to do, as I have to write a bunch of code to detect the user's package manager, and then write code to abstract away the differences between the package managers. This package aims to solve that problem.

Right about now, you might be thinking "Why not use which-pm or which-pm-runs or preferred-pm". Well, those packages are great, but they don't abstract away the differences between the package managers. This package does that.

Detection logic

  • If a bin target is executed with a user_agent i.e npx | pnpx | yarn dlx, then the package manager is detected from the user_agent.
  • If a lockfile is present, then the package manager is detected from the lockfile.
  • If node_modules is present, then the package manager is detected from the node_modules.
  • If multiple package managers are detected and the prefer option has been passed, then the package manager is detected from the prefer option. see example below.
  • If all else fails, defaults to npm.

Usage

import { getPackageManager } from 'get-pm';

const pm = getPackageManager({
    prefer: ['pnpm', 'yarn'], // optional - if multiple found, prefer these
    cwd: process.cwd(), // optional - defaults to process.cwd()
    workspace: true, // optional - treat working dir as a workspace
    silent: false, // optional - suppress console output when running pm commands eg. `pm.install()`
});

console.log(pm.name); // npm | yarn | pnpm
console.log(pm.version) // semver version of the package manager

// install packages defined in package.json
await pm.install();

// add packages
await pm.add(['foo', 'bar']);

// add packages with flags/options
await pm.add(['foo', 'bar'], "-Dw"); // -Dw is the workspace flag for pnpm

// remove packages
await pm.remove(['foo', 'bar']);

// run scripts in package.json
await pm.run('build');

// execute binaries
await pm.exec('tailwindcss', ['init', '-p']);

Usage with commonjs

The get-pm package is an esm module which cannot be used with the default require syntax in commonjs. To use it with cjs, you will have to use the import syntax as shown below:

async function getPm() {
    const { getPackageManager } = await import("get-pm")
    const pm = getPackageManager();

    return pm
}

License

This project is licensed under the MIT License - see the LICENSE file for details