@dephub/package-check
v1.0.3
Published
Check if packages are installed locally, in workspace, or globally
Maintainers
Readme
@dephub/package-check
Check if packages are installed locally, in workspace, or globally
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-checkCLI 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 npmOptions
--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 checkoptions(CheckOptions) - Optional configurationscope- 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 checkoptions(object) - Optional configurationcwd- 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 checkoptions(CheckOptions) - Optional configuration
Returns: Promise<boolean>
packageChecker.checkGlobal(pkg, options?)
Check if a package is installed globally.
Parameters:
pkg(string) - Package name to checkoptions(object) - Optional configurationglobalPaths- 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.resolvefor 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)
