Skip to content

dephub-js/package-check

Repository files navigation

@dephub/package-check

Check if packages are installed locally, in workspace, or globally

NPM version ESM-only

Features ✨

  • πŸ” Multi-scope Checking - Check packages in local, workspace, or global scope
  • 🎯 Smart Resolution - Uses Node.js resolution for comprehensive package detection
  • πŸš€ Zero Dependencies - Only uses @dephub packages for core functionality
  • πŸ”’ Type Safe - Full TypeScript support with strict types
  • πŸ’» CLI & API - Use via command line or programmatically

Installation πŸ’Ώ

# Using npm
npm install @dephub/package-check

# Using pnpm
pnpm add @dephub/package-check

# Using yarn
yarn add @dephub/package-check

# Using bun
bun add @dephub/package-check

CLI Usage πŸ–₯️

Basic Commands

# Check if a package is installed (auto scope)
package-check check eslint

# Check locally installed package
package-check local typescript

# Check workspace package (monorepo support)
package-check workspace react

# Check globally installed package
package-check global npm

Options

  • --cwd <path> - Working directory for checking
  • --scope <scope> - Check scope: local, workspace, global, auto
  • --rootOnly - Check only the root package (not nested dependencies)
  • --globalPaths <paths> - Additional global paths to check

Examples

# Check with specific directory
package-check check eslint --cwd /path/to/project

# Check only root-level dependencies
package-check check typescript --rootOnly

# Check in workspace scope
package-check check react --scope workspace

# Check with custom global paths
package-check global npm --globalPaths "/usr/lib/node_modules,/custom/path"

Programmatic Usage πŸ› οΈ

import { packageChecker } from '@dephub/package-check';

// Check if package is installed (auto scope)
const isInstalled = await packageChecker.check('eslint');
console.log(isInstalled); // true or false

// Check specific scope
const isLocal = await packageChecker.checkLocal('typescript');
const isWorkspace = await packageChecker.checkWorkspace('react');
const isGlobal = await packageChecker.checkGlobal('npm');

// Check with options
const isAvailable = await packageChecker.check('eslint', {
  scope: 'workspace',
  cwd: '/path/to/project',
  rootOnly: true,
});

API Reference πŸ“š

packageChecker.check(pkg, options?)

Check if a package is installed in the specified scope.

Parameters:

  • pkg (string) - Package name to check
  • options (CheckOptions) - Optional configuration
    • scope - Check scope: 'local', 'workspace', 'global', or 'auto' (default: 'auto')
    • cwd - Working directory for checking (default: process.cwd())
    • rootOnly - Enforce package must be in root-level node_modules (default: false)
    • globalPaths - Additional global paths to check

Returns: Promise<boolean> - True if package is installed

packageChecker.checkLocal(pkg, options?)

Check if a package is installed locally.

Parameters:

  • pkg (string) - Package name to check
  • options (object) - Optional configuration
    • cwd - Working directory (default: process.cwd())
    • rootOnly - Check only root-level package (default: false)

Returns: Promise<boolean>

packageChecker.checkWorkspace(pkg, options?)

Check if a package is installed in workspace.

Parameters:

  • pkg (string) - Package name to check
  • options (CheckOptions) - Optional configuration

Returns: Promise<boolean>

packageChecker.checkGlobal(pkg, options?)

Check if a package is installed globally.

Parameters:

  • pkg (string) - Package name to check
  • options (object) - Optional configuration
    • globalPaths - Additional global paths to check

Returns: Promise<boolean>

Check Scopes πŸ”Ž

  • local: Check in current directory using Node.js resolution
  • workspace: Check in current and parent directories (monorepo support)
  • global: Check in global package manager directories
  • auto: Check local β†’ workspace β†’ global (default)

Root-Only Mode 🎯

When rootOnly: true is enabled:

  • Only checks direct dependencies in node_modules/package-name
  • Ignores nested dependencies and pnpm store (.pnpm directory)
  • Useful for verifying direct project dependencies

When rootOnly: false (default):

  • Uses Node.js require.resolve for comprehensive detection
  • Finds packages anywhere in node_modules hierarchy
  • Works with all package managers (npm, yarn, pnpm, bun)

Examples πŸ’‘

Basic Checking

// Quick check in auto scope
const hasEslint = await packageChecker.check('eslint');

// Scope-specific checks
const hasLocal = await packageChecker.checkLocal('typescript');
const hasWorkspace = await packageChecker.checkWorkspace('react');
const hasGlobal = await packageChecker.checkGlobal('npm');

Advanced Usage

// Check only direct dependencies
const isDirectDep = await packageChecker.checkLocal('eslint', {
  rootOnly: true,
});

// Check in specific directory structure
const hasPackage = await packageChecker.check('vue', {
  scope: 'workspace',
  cwd: '/path/to/monorepo',
});

// Check with custom global paths
const hasGlobalPkg = await packageChecker.checkGlobal('custom-pm', {
  globalPaths: ['/custom/global/path'],
});

Integration in Tools

// Verify required tools are available
const requiredTools = ['typescript', 'eslint', 'prettier'];
for (const tool of requiredTools) {
  if (!(await packageChecker.check(tool))) {
    throw new Error(`Required tool ${tool} is not installed`);
  }
}

Types

CheckScope

type CheckScope = 'local' | 'workspace' | 'global' | 'auto';

CheckOptions

interface CheckOptions {
  globalPaths?: string[];
  scope?: CheckScope;
  cwd?: string;
  rootOnly?: boolean;
}

License πŸ“„

MIT License

Author: Estarlin R (estarlincito.com)

About

Check if packages are installed locally, in workspace, or globally

Topics

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors