Get information on local packages, providing utilities for package detection, metadata retrieval, module resolution, and cross-environment imports.
npx @tessl/cli install tessl/npm-local-pkg@1.1.0local-pkg provides utilities for working with local npm packages, offering functions to detect package existence, retrieve package information, resolve module paths, and import modules in both CommonJS and ESM environments. It includes comprehensive cross-environment compatibility and works with PnP (Plug'n'Play) package managers.
npm install local-pkgimport {
isPackageExists,
getPackageInfo,
getPackageInfoSync,
resolveModule,
importModule,
loadPackageJSON,
loadPackageJSONSync,
isPackageListed,
isPackageListedSync,
type PackageInfo,
type PackageResolvingOptions
} from "local-pkg";
import type { PackageJson } from "pkg-types";For CommonJS:
const {
isPackageExists,
getPackageInfo,
getPackageInfoSync,
resolveModule,
importModule,
loadPackageJSON,
loadPackageJSONSync,
isPackageListed,
isPackageListedSync
} = require("local-pkg");import { isPackageExists, getPackageInfo, resolveModule, importModule } from "local-pkg";
// Check if package exists
const exists = isPackageExists("express");
console.log(exists); // true or false
// Get package information
const packageInfo = await getPackageInfo("express");
if (packageInfo) {
console.log(packageInfo.name); // "express"
console.log(packageInfo.version); // "4.18.2"
console.log(packageInfo.rootPath); // "/path/to/node_modules/express"
}
// Resolve module path (similar to require.resolve)
const modulePath = resolveModule("express");
console.log(modulePath); // "/path/to/node_modules/express/index.js"
// Dynamic import that works in both CJS and ESM
const express = await importModule("express");local-pkg is built around several key components:
Core functionality for checking if packages exist and resolving their locations in the local environment.
function isPackageExists(name: string, options?: PackageResolvingOptions): boolean;
function resolveModule(name: string, options?: PackageResolvingOptions): string | undefined;
interface PackageResolvingOptions {
paths?: string[];
platform?: 'posix' | 'win32' | 'auto';
}Retrieve comprehensive metadata and information about installed packages, including version, paths, and full package.json contents.
function getPackageInfo(name: string, options?: PackageResolvingOptions): Promise<PackageInfo | undefined>;
function getPackageInfoSync(name: string, options?: PackageResolvingOptions): PackageInfo | undefined;
interface PackageInfo {
name: string;
rootPath: string;
packageJsonPath: string;
version: string;
packageJson: PackageJson;
}Cross-environment dynamic module importing that works consistently in both CommonJS and ESM contexts.
function importModule<T = any>(path: string): Promise<T>;Load and parse package.json files from the current working directory or specified paths, with dependency checking capabilities.
function loadPackageJSON(cwd?: string): Promise<PackageJson | null>;
function loadPackageJSONSync(cwd?: string): PackageJson | null;
function isPackageListed(name: string, cwd?: string): Promise<boolean>;
function isPackageListedSync(name: string, cwd?: string): boolean;interface PackageInfo {
name: string;
rootPath: string;
packageJsonPath: string;
version: string;
packageJson: PackageJson;
}
interface PackageResolvingOptions {
paths?: string[];
/**
* @default 'auto'
* Resolve path as posix or win32
*/
platform?: 'posix' | 'win32' | 'auto';
}
// Note: PackageJson type is imported from 'pkg-types' package
// Contains standard package.json fields like name, version, dependencies, etc.
type PackageJson = import('pkg-types').PackageJson;