0
# Knip
1
2
Knip 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.
3
4
## Package Information
5
6
- **Package Name**: knip
7
- **Package Type**: npm
8
- **Language**: TypeScript
9
- **Installation**: `npm install knip`
10
11
## Core Imports
12
13
```typescript
14
import { main, type KnipConfig } from "knip";
15
```
16
17
For CommonJS:
18
19
```javascript
20
const { main } = require("knip");
21
```
22
23
## Basic Usage
24
25
### CLI Usage
26
27
```bash
28
# Analyze current project
29
npx knip
30
31
# Analyze with configuration file
32
npx knip --config knip.json
33
34
# Fix issues automatically
35
npx knip --fix
36
37
# Production analysis only
38
npx knip --production
39
40
# Watch mode for continuous analysis
41
npx knip --watch
42
```
43
44
### Programmatic Usage
45
46
```typescript
47
import { main } from "knip";
48
49
// Basic analysis
50
const results = await main({
51
cwd: process.cwd(),
52
gitignore: true,
53
isProduction: false,
54
isShowProgress: true,
55
});
56
57
console.log(`Found ${results.counters.total} issues`);
58
console.log(`Unused files: ${results.counters.files}`);
59
console.log(`Unused dependencies: ${results.counters.dependencies}`);
60
```
61
62
## Architecture
63
64
Knip is built around several key components:
65
66
- **Analysis Engine**: Core functionality for scanning projects and building dependency graphs
67
- **Plugin System**: 129+ plugins for framework and tool-specific analysis
68
- **Configuration System**: Flexible workspace and project configuration
69
- **Issue Management**: Collection, categorization, and fixing of identified issues
70
- **Reporting System**: Multiple output formats for different workflows
71
- **CLI Interface**: Comprehensive command-line tool with extensive options
72
73
## Capabilities
74
75
### Core Analysis API
76
77
Main programmatic interface for analyzing projects and detecting unused code, dependencies, and files.
78
79
```typescript { .api }
80
function main(options: MainOptions): Promise<MainResult>;
81
82
interface MainOptions {
83
cwd: string;
84
gitignore?: boolean;
85
isProduction?: boolean;
86
isShowProgress?: boolean;
87
includedIssueTypes?: Partial<Report>;
88
// ... additional options
89
}
90
91
interface MainResult {
92
issues: Issues;
93
counters: Counters;
94
tagHints: Set<TagHint>;
95
configurationHints: Set<ConfigurationHint>;
96
includedWorkspaceDirs: string[];
97
}
98
```
99
100
[Core Analysis](./analysis.md)
101
102
### Configuration Management
103
104
Flexible configuration system supporting project-level and workspace-level settings with extensive customization options.
105
106
```typescript { .api }
107
type KnipConfig = {
108
entry?: string | string[];
109
project?: string | string[];
110
ignore?: string | string[];
111
ignoreBinaries?: (string | RegExp)[];
112
ignoreDependencies?: (string | RegExp)[];
113
workspaces?: Record<string, WorkspaceConfiguration>;
114
// ... additional configuration options
115
};
116
```
117
118
[Configuration](./configuration.md)
119
120
### Plugin System
121
122
Extensive plugin architecture supporting 129+ tools and frameworks with automatic detection and configuration.
123
124
```typescript { .api }
125
interface Plugin {
126
title: string;
127
enablers?: string | (string | RegExp)[];
128
isEnabled?: (options: PluginOptions) => boolean | Promise<boolean>;
129
config?: string[];
130
entry?: string[];
131
project?: string[];
132
// ... additional plugin properties
133
}
134
```
135
136
[Plugins](./plugins.md)
137
138
### Issue Detection & Management
139
140
Comprehensive issue detection system categorizing different types of unused code and dependency problems.
141
142
```typescript { .api }
143
interface Issues {
144
files: Set<string>;
145
dependencies: Record<string, Record<string, Issue>>;
146
devDependencies: Record<string, Record<string, Issue>>;
147
exports: Record<string, Record<string, Issue>>;
148
types: Record<string, Record<string, Issue>>;
149
unresolved: Record<string, Record<string, Issue>>;
150
// ... additional issue categories
151
}
152
153
enum SymbolType {
154
VARIABLE = 'variable',
155
TYPE = 'type',
156
INTERFACE = 'interface',
157
ENUM = 'enum',
158
FUNCTION = 'function',
159
CLASS = 'class',
160
MEMBER = 'member',
161
UNKNOWN = 'unknown'
162
}
163
```
164
165
[Issue Management](./issues.md)
166
167
### Reporting & Output
168
169
Multiple reporting formats and customizable output options for different workflows and CI/CD integration.
170
171
```typescript { .api }
172
interface ReporterOptions {
173
report: Report;
174
issues: Issues;
175
counters: Counters;
176
cwd: string;
177
isProduction: boolean;
178
options: string;
179
}
180
181
type Reporter = (options: ReporterOptions) => void;
182
```
183
184
[Reporting](./reporting.md)
185
186
### CLI Interface
187
188
Comprehensive command-line interface with extensive options for analysis, fixing, and workflow integration.
189
190
```typescript { .api }
191
interface CLIOptions {
192
config?: string;
193
tsConfig?: string;
194
production?: boolean;
195
strict?: boolean;
196
workspace?: string;
197
directory?: string;
198
cache?: boolean;
199
watch?: boolean;
200
fix?: boolean;
201
// ... additional CLI options
202
}
203
```
204
205
[CLI Usage](./cli.md)
206
207
## Types
208
209
```typescript { .api }
210
interface Issue {
211
type: SymbolIssueType;
212
filePath: string;
213
workspace: string;
214
symbol: string;
215
symbolType?: SymbolType;
216
pos?: number;
217
line?: number;
218
col?: number;
219
}
220
221
interface Counters {
222
files: number;
223
dependencies: number;
224
devDependencies: number;
225
exports: number;
226
types: number;
227
unresolved: number;
228
processed: number;
229
total: number;
230
}
231
232
interface Report {
233
files: boolean;
234
dependencies: boolean;
235
devDependencies: boolean;
236
exports: boolean;
237
types: boolean;
238
unresolved: boolean;
239
// ... additional report flags
240
}
241
242
interface ConfigurationHint {
243
type: ConfigurationHintType;
244
identifier: string | RegExp;
245
filePath?: string;
246
workspaceName?: string;
247
}
248
249
interface TagHint {
250
type: 'tag';
251
filePath: string;
252
identifier: string;
253
tagName: string;
254
}
255
```