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 🙏

© 2025 – Pkg Stats / Ryan Hefner

pm-ninja

v1.1.3

Published

A utility library for managing Node/bun package managers

Readme

📦 pm-ninja

A versatile utility package for managing Node/bun js package installations and script execution across multiple package managers (npm, yarn, pnpm, and bun).

Features

  • 🔍 Automatic package manager detection
  • 📦 Smart dependency installation
  • 🛠️ Cross-platform script execution
  • 📝 Package.json management utilities
  • 🚀 Support for npm, yarn, pnpm, and bun

Installation

npm install pm-ninja
# or
yarn add pm-ninja
# or
pnpm add pm-ninja
# or
bun add pm-ninja

Usage

Detecting Package Manager

import { getPackageManager } from 'pm-ninja';

const pm = await getPackageManager();
// Returns: 'npm' | 'yarn' | 'pnpm' | 'bun' | null

Installing Packages

import { installPackages } from 'pm-ninja';

await installPackages({
  dependencies: ['express', 'axios'],
  devDependencies: ['typescript', '@types/node']
});

Running Scripts

import { runScript } from 'pm-ninja';

// Run a script defined in package.json
await runScript('build');

// Run with additional arguments
await runScript('test', ['--watch']);

// Run with additional options
await runScript('start', [], {
  cwd: './packages/frontend',  // Run in a specific directory
  pm: 'yarn',                  // Force a specific package manager
  silent: true                 // Suppress console output
});

Checking Installed Packages

import { getInstalledPackages } from 'pm-ninja';

const packages = await getInstalledPackages();
console.log('Dependencies:', packages.dependencies);
console.log('Dev Dependencies:', packages.devDependencies);

Checking Missing Packages

import { checkMissingPackages } from 'pm-ninja';

const missingPackages = await checkMissingPackages({
  dependencies: ['express', 'react'],
  devDependencies: ['typescript', 'jest']
});

if (missingPackages.dependencies.length > 0) {
  console.log('Missing dependencies:', missingPackages.dependencies);
}
if (missingPackages.devDependencies.length > 0) {
  console.log('Missing dev dependencies:', missingPackages.devDependencies);
}

Reading and Writing package.json

import { readPackageJson, writePackageJson } from 'pm-ninja';

// Read package.json
const packageJson = await readPackageJson();

// Modify package.json content
packageJson.version = '1.0.1';

// Write back to package.json
await writePackageJson(packageJson);

Managing package.json Scripts

import { addScripts } from 'pm-ninja';

await addScripts({
  'build': 'tsc',
  'dev': 'nodemon src/index.ts',
  'test': 'jest'
});

API Reference

getPackageManager(cwd?: string): Promise<PM | null>

Detects the package manager being used in the project by checking for lock files.

  • cwd: Current working directory (defaults to ".")
  • Returns: Promise resolving to "npm", "yarn", "pnpm", "bun", or null

installPackages(config: PackageInstallConfig): Promise<void>

Installs missing packages using the detected package manager.

interface PackageInstallConfig {
  dependencies: string[];
  devDependencies: string[];
}

runScript(script: string, args?: string[], options?: RunScriptOptions): Promise<ExecResult>

Runs a package.json script using the detected package manager.

  • script: Name of the script to run
  • args: Additional arguments to pass to the script (defaults to [])
  • options: Additional options (optional)
interface RunScriptOptions {
  cwd?: string;    // Current working directory (defaults to ".")
  pm?: PM;         // Specify package manager, overrides auto-detection
  silent?: boolean; // Suppress console output (defaults to false)
}

getInstalledPackages(): Promise<InstalledPackages>

Returns an object containing all installed dependencies and devDependencies from package.json.

interface InstalledPackages {
  dependencies: Record<string, string>;
  devDependencies: Record<string, string>;
}

checkMissingPackages(config: PackageInstallConfig): Promise<MissingPackages>

Checks which required packages are not installed.

interface PackageInstallConfig {
  dependencies: string[];
  devDependencies: string[];
}

interface MissingPackages {
  dependencies: string[];
  devDependencies: string[];
}

readPackageJson(): Promise<PackageJson>

Reads and parses the package.json file.

  • Returns: Promise resolving to the parsed package.json content
  • Throws: Error if package.json cannot be read or parsed

writePackageJson(packageJson: PackageJson): Promise<void>

Writes content to package.json file.

  • packageJson: The package.json content to write
  • Throws: Error if writing to package.json fails

addScripts(scripts: PackageJsonScripts): Promise<void>

Adds or updates scripts in package.json.

interface PackageJsonScripts {
  [key: string]: string;
}

Error Handling

The package includes comprehensive error handling for common scenarios:

  • No package manager detected
  • Missing package.json
  • Failed installations
  • Script execution errors

Example:

try {
  await installPackages({
    dependencies: ['express'],
    devDependencies: ['typescript']
  });
} catch (error) {
  console.error('Installation failed:', error.message);
}

🤝 Contributing

Contributions are welcome! Check back soon for contribution guidelines.

📝 License

This project is licensed under the MIT License .

Acknowledgments

  • Built with TypeScript
  • Inspired by the need for unified package management across different package managers