Knip
Knip is a static analysis tool designed to identify and resolve unused dependencies, exports, and files within JavaScript and TypeScript projects. Currently at version 6.4.1, the project maintains an active release cadence, frequently releasing patch and minor updates to enhance plugin support for various ecosystems, including Astro, Vitest, Rspack, Panda CSS, Rolldown, Stencil, Metro, and Storybook, and to improve its core dependency resolution logic. Knip differentiates itself by providing a comprehensive, configurable solution for dead code elimination, which directly contributes to reduced bundle sizes, improved application performance, and streamlined project maintenance through easier refactorings. Its primary function is invoked via a command-line interface, providing clear reports on unused assets. Additionally, Knip exposes types such as `KnipConfiguration` and a `defineConfig` utility for programmatic configuration scenarios, aiding in its integration with advanced build pipelines, CI/CD systems, and custom development tooling, ensuring type-safe configuration. It aims to reduce technical debt by accurately pinpointing unused code and resources.
Common errors
-
Error: Knip requires Node.js version ^20.19.0 || >=22.12.0.
cause The installed Node.js version on your system does not meet the minimum requirements for Knip.fixUpgrade your Node.js installation to version `20.19.0` or higher, or `22.12.0` or higher (e.g., `nvm install 20 && nvm use 20`). -
Entry file '/path/to/my/component.tsx' is not found. Please check your configuration.
cause A file specified in your `knip.ts` `entry` or `project` configuration does not exist or the glob pattern is incorrect.fixReview the `entry` and `project` globs in your `knip.ts` file to ensure they correctly match existing files and directories. Double-check for typos or incorrect paths relative to the project root. -
Knip reported dependency 'my-package' as unused, but I'm actively using it in my project.
cause Knip's analysis might not fully understand how 'my-package' is being used due to an unconfigured plugin, complex import patterns, or a specific framework's behavior.fixEnsure that the relevant Knip plugin for your framework (e.g., `next`, `astro`) is enabled and configured. If a plugin doesn't exist or isn't sufficient, consider adding an `ignoreDependencies` entry for 'my-package' in your `knip.ts`.
Warnings
- breaking Knip requires specific Node.js versions. Running with older versions will result in an error and prevent execution.
- gotcha Incorrect or incomplete configuration of entry points (`entry`) or project files (`project`) in `knip.ts` can lead to false positives (reporting used code as unused) or false negatives (missing actual dead code). This is especially common in complex monorepos or projects with dynamic imports.
- gotcha Knip relies on plugins for accurate analysis of specific frameworks, libraries, and build tools. If a relevant plugin is not enabled or properly configured for your project's ecosystem (e.g., React, Vitest, Storybook, Next.js), it may report false positives for dependencies or code used by that technology.
Install
-
npm install knip -
yarn add knip -
pnpm add knip
Imports
- defineConfig
const defineConfig = require('knip').defineConfig;import { defineConfig } from 'knip'; - KnipConfig
import { KnipConfig } from 'knip';import { type KnipConfig } from 'knip';
Quickstart
{
// package.json
"name": "my-project",
"version": "1.0.0",
"scripts": {
"knip": "knip"
},
"devDependencies": {
"knip": "^6.0.0"
},
"dependencies": {
"react": "18.2.0" // Example: Will be marked as unused by Knip
}
}
// knip.ts (create this file in your project root)
import { defineConfig, type KnipConfig } from 'knip';
const config: KnipConfig = defineConfig({
// Specify entry files for your application
entry: ['src/index.ts', 'src/**/*.{jsx,tsx}'],
// Specify project files to analyze
project: ['src/**/*.{ts,tsx,js,jsx}'],
// Ignore specific files or patterns (e.g., test files, generated code)
ignore: ['src/**/*.test.ts', 'dist/**/*', 'node_modules/**/*'],
// Enable or disable specific rules, or set thresholds (error, warn, off)
rules: {
unusedDependencies: 'error',
unusedFiles: 'error',
unusedExports: 'warn'
},
// Enable plugins for specific ecosystems (e.g., React, Vitest)
plugins: {
react: true,
typescript: { config: 'tsconfig.json' } // Default, but can be explicit
}
});
export default config;
// To run Knip:
// npm install --save-dev knip
// npm run knip