Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects
npx @tessl/cli install tessl/npm-knip@5.63.0Knip is a comprehensive TypeScript and JavaScript analysis tool that automatically detects and helps fix unused dependencies, exports, and files in software projects. It provides both a command-line interface and programmatic API for analyzing codebases to identify dead code, unused dependencies, unreferenced exports, and unnecessary files.
npm install knipimport { main, type KnipConfig } from "knip";For CommonJS:
const { main } = require("knip");# Analyze current project
npx knip
# Analyze with configuration file
npx knip --config knip.json
# Fix issues automatically
npx knip --fix
# Production analysis only
npx knip --production
# Watch mode for continuous analysis
npx knip --watchimport { main } from "knip";
// Basic analysis
const results = await main({
cwd: process.cwd(),
gitignore: true,
isProduction: false,
isShowProgress: true,
});
console.log(`Found ${results.counters.total} issues`);
console.log(`Unused files: ${results.counters.files}`);
console.log(`Unused dependencies: ${results.counters.dependencies}`);Knip is built around several key components:
Main programmatic interface for analyzing projects and detecting unused code, dependencies, and files.
function main(options: MainOptions): Promise<MainResult>;
interface MainOptions {
cwd: string;
gitignore?: boolean;
isProduction?: boolean;
isShowProgress?: boolean;
includedIssueTypes?: Partial<Report>;
// ... additional options
}
interface MainResult {
issues: Issues;
counters: Counters;
tagHints: Set<TagHint>;
configurationHints: Set<ConfigurationHint>;
includedWorkspaceDirs: string[];
}Flexible configuration system supporting project-level and workspace-level settings with extensive customization options.
type KnipConfig = {
entry?: string | string[];
project?: string | string[];
ignore?: string | string[];
ignoreBinaries?: (string | RegExp)[];
ignoreDependencies?: (string | RegExp)[];
workspaces?: Record<string, WorkspaceConfiguration>;
// ... additional configuration options
};Extensive plugin architecture supporting 129+ tools and frameworks with automatic detection and configuration.
interface Plugin {
title: string;
enablers?: string | (string | RegExp)[];
isEnabled?: (options: PluginOptions) => boolean | Promise<boolean>;
config?: string[];
entry?: string[];
project?: string[];
// ... additional plugin properties
}Comprehensive issue detection system categorizing different types of unused code and dependency problems.
interface Issues {
files: Set<string>;
dependencies: Record<string, Record<string, Issue>>;
devDependencies: Record<string, Record<string, Issue>>;
exports: Record<string, Record<string, Issue>>;
types: Record<string, Record<string, Issue>>;
unresolved: Record<string, Record<string, Issue>>;
// ... additional issue categories
}
enum SymbolType {
VARIABLE = 'variable',
TYPE = 'type',
INTERFACE = 'interface',
ENUM = 'enum',
FUNCTION = 'function',
CLASS = 'class',
MEMBER = 'member',
UNKNOWN = 'unknown'
}Multiple reporting formats and customizable output options for different workflows and CI/CD integration.
interface ReporterOptions {
report: Report;
issues: Issues;
counters: Counters;
cwd: string;
isProduction: boolean;
options: string;
}
type Reporter = (options: ReporterOptions) => void;Comprehensive command-line interface with extensive options for analysis, fixing, and workflow integration.
interface CLIOptions {
config?: string;
tsConfig?: string;
production?: boolean;
strict?: boolean;
workspace?: string;
directory?: string;
cache?: boolean;
watch?: boolean;
fix?: boolean;
// ... additional CLI options
}interface Issue {
type: SymbolIssueType;
filePath: string;
workspace: string;
symbol: string;
symbolType?: SymbolType;
pos?: number;
line?: number;
col?: number;
}
interface Counters {
files: number;
dependencies: number;
devDependencies: number;
exports: number;
types: number;
unresolved: number;
processed: number;
total: number;
}
interface Report {
files: boolean;
dependencies: boolean;
devDependencies: boolean;
exports: boolean;
types: boolean;
unresolved: boolean;
// ... additional report flags
}
interface ConfigurationHint {
type: ConfigurationHintType;
identifier: string | RegExp;
filePath?: string;
workspaceName?: string;
}
interface TagHint {
type: 'tag';
filePath: string;
identifier: string;
tagName: string;
}