Knip

6.4.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

Sets up Knip in a TypeScript project with a `knip.ts` configuration, defines basic entry points and rules, and demonstrates running it via `npm run knip`.

{
  // 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

view raw JSON →