or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.mdmodule-importing.mdpackage-detection.mdpackage-information.mdpackage-json-management.md
tile.json

tessl/npm-local-pkg

Get information on local packages, providing utilities for package detection, metadata retrieval, module resolution, and cross-environment imports.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/local-pkg@1.1.x

To install, run

npx @tessl/cli install tessl/npm-local-pkg@1.1.0

index.mddocs/

local-pkg

local-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.

Package Information

  • Package Name: local-pkg
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install local-pkg

Core Imports

import {
  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");

Basic Usage

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");

Architecture

local-pkg is built around several key components:

  • Package Detection: Core functions to check package existence and resolve package paths
  • Information Retrieval: Extract comprehensive metadata from packages and package.json files
  • Module Resolution: Cross-environment module path resolution similar to require.resolve
  • Dynamic Imports: Unified import functionality that works in both CommonJS and ESM contexts
  • Synchronous/Asynchronous APIs: Dual sync/async versions for key operations to support different use cases
  • Platform Compatibility: Handles platform-specific path formats and PnP package managers

Capabilities

Package Detection

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';
}

Package Detection

Package Information

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;
}

Package Information

Module Importing

Cross-environment dynamic module importing that works consistently in both CommonJS and ESM contexts.

function importModule<T = any>(path: string): Promise<T>;

Module Importing

Package.json Management

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;

Package.json Management

Types

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;