vite-plugin-import-graph

raw JSON →
0.0.1 verified Mon Apr 27 auth: no javascript

A Vite plugin that records a project's module import graph as a JSON file. Version 0.0.1 is the initial stable release. It focuses purely on data output without a visualization frontend, making it faster than integrated tools. It supports options like absolute/relative module IDs and prefixing virtual/npm modules. Output is a JSON object mapping module IDs to arrays of imported IDs, with npm package paths merged at the package level. The plugin is lightweight and designed for downstream use with visualization (D3.js, Graphviz) or analysis (circular dependencies, dead code, test impact).

error Cannot find module 'vite-plugin-import-graph'
cause Package not installed or not running in Vite context.
fix
Run npm i -D vite-plugin-import-graph and ensure Vite dev/build is executed in the same project.
error TypeError: importGraph is not a function
cause Using named import instead of default import.
fix
Change to import importGraph from 'vite-plugin-import-graph'.
error Error: [vite-plugin-import-graph] Vite >= 5 required
cause Installed Vite version is older than 5.
fix
Upgrade Vite to ^5.0.0: npm i vite@^5.0.0 -D.
gotcha Output file is written relative to project root, not cwd.
fix Specify an absolute path for `filename` if needed.
gotcha Npm package paths are merged at the package level; individual file paths within a package are not recorded.
fix Use `absoluteModuleIds: true` with custom resolution if you need per-file granularity.
gotcha usePrefix only adds prefixes to virtual modules and npm packages; other module IDs remain unchanged.
fix Enable `usePrefix` only if you need to distinguish virtual/npm modules.
npm install vite-plugin-import-graph
yarn add vite-plugin-import-graph
pnpm add vite-plugin-import-graph

Configure the plugin with default options to generate import graph JSON.

// vite.config.ts
import { defineConfig } from 'vite';
import importGraph from 'vite-plugin-import-graph';

export default defineConfig({
  plugins: [
    importGraph({
      filename: 'import-graph.json',
      absoluteModuleIds: false,
      usePrefix: false,
    })
  ]
});