Package and TypeScript Configuration Utilities

2.3.0 · active · verified Sun Apr 19

pkg-types is a utility library providing Node.js functions and comprehensive TypeScript definitions for common project configuration files, including `package.json`, `tsconfig.json`, and various lock files (e.g., `yarn.lock`, `pnpm-lock.yaml`, `bun.lockb`, `deno.lock`). The current stable version is 2.3.0. The package has an active development cycle, with frequent releases bringing enhancements and bug fixes. A key differentiator is its automatic format detection for `package.json` (supporting `.json`, `.json5`, `.yaml`) and robust utilities for finding and reading workspace configurations across different package managers and monorepo tools like Lerna, Turborepo, and Rush. It streamlines the process of programmatically interacting with project metadata, making it ideal for tooling development.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates how to find and read project `package.json` and `tsconfig.json` files, and detect the workspace root directory using various utilities from `pkg-types`.

import { readPackage, findPackage, readTSConfig, findWorkspaceDir } from 'pkg-types';
import path from 'node:path';

async function getProjectConfig() {
  try {
    // Find the nearest package.json, package.json5, or package.yaml
    const packageFile = await findPackage(process.cwd());
    if (packageFile) {
      console.log(`Found package file: ${packageFile}`);

      // Read the package configuration, automatically detecting format
      const pkg = await readPackage(path.dirname(packageFile));
      console.log('Project Name:', pkg.name);
      console.log('Project Version:', pkg.version);
    } else {
      console.log('No package file found in current directory or ancestors.');
    }

    // Read the nearest tsconfig.json
    const tsconfigPath = await findPackage(process.cwd(), { filenames: ['tsconfig.json'] });
    if (tsconfigPath) {
      const tsconfig = await readTSConfig(path.dirname(tsconfigPath));
      console.log('TSConfig Compiler Options:', tsconfig?.compilerOptions);
    } else {
      console.log('No tsconfig.json found.');
    }

    // Find the workspace root directory
    const workspaceRoot = await findWorkspaceDir(process.cwd());
    if (workspaceRoot) {
      console.log('Detected workspace root:', workspaceRoot);
    } else {
      console.log('No workspace root detected.');
    }

  } catch (error) {
    console.error('Failed to read project configuration:', (error as Error).message);
  }
}

getProjectConfig();

view raw JSON →