DPDM: Dependency Analyzer

4.0.1 · active · verified Sun Apr 19

dpdm is a robust static dependency analyzer designed for JavaScript and TypeScript projects. It efficiently identifies circular dependencies, detects unused files, and generates comprehensive dependency trees. Currently at stable version 4.0.1, the library maintains a relatively frequent release cadence, often aligning minor versions with updates to TypeScript and Node.js to ensure compatibility with modern language features. Key differentiators include its complete support for both CommonJS and ESM module systems, comprehensive TypeScript features such as path mapping resolution and the ability to ignore type-only dependencies, and a lightweight architecture that leverages the official TypeScript compiler for parsing. It aims to provide stable and consistent output, addressing inconsistencies found in alternative tools like `madge` when analyzing complex TypeScript projects.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `dpdm` programmatically to analyze a project for circular dependencies and unused files, showing basic configuration and result handling.

import { analyze } from 'dpdm';
import * as fs from 'node:fs';
import * as path from 'node:path';

const projectRoot = process.cwd();
const srcDir = path.join(projectRoot, 'src');

// Create dummy files for demonstration
fs.mkdirSync(srcDir, { recursive: true });
fs.writeFileSync(path.join(srcDir, 'a.ts'), 'import { b } from "./b"; export const a = b + 1;');
fs.writeFileSync(path.join(srcDir, 'b.ts'), 'import { c } from "./c"; export const b = c + 1;');
fs.writeFileSync(path.join(srcDir, 'c.ts'), 'import { a } from "./a"; export const c = a + 1;'); // Circular dependency
fs.writeFileSync(path.join(srcDir, 'd.ts'), 'export const d = 4;'); // Unused file

async function runAnalysis() {
  try {
    const result = await analyze([path.join(srcDir, 'a.ts')], {
      context: projectRoot,
      exclude: 'node_modules',
      circular: true,
      tree: true,
      detectUnusedFilesFrom: path.join(srcDir, '**/*.*')
    });

    console.log('Analysis completed:');
    if (result.circular.length > 0) {
      console.log('Circular dependencies found:', result.circular);
    } else {
      console.log('No circular dependencies found.');
    }
    if (result.unused.length > 0) {
      console.log('Unused files found:', result.unused);
    }
    console.log('Full dependency tree (truncated for brevity):', Object.keys(result.tree).slice(0, 5));
  } catch (error) {
    console.error('Analysis failed:', error);
  } finally {
    // Clean up dummy files
    fs.rmSync(srcDir, { recursive: true, force: true });
  }
}

runAnalysis();

view raw JSON →