### v0.13.3 Configuration Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/migration-to-v1.md An example of a configuration object in v0.13.3 or before. ```json { "entryFiles": ["src/index.ts"], "projectFiles": ["src/**/*.ts", "!**/*.spec.ts"], "dev": { "entryFiles": ["src/index.ts", "src/**/*.spec.ts", "src/**/*.e2e.ts"], "projectFiles": ["src/**/*.ts"] } } ``` -------------------------------- ### Example Resulting Package.json Source: https://github.com/webpro-nl/knip/blob/main/packages/create-config/README.md An example of how the package.json might look after running the initialization command. ```json { "name": "my-package", "scripts": { "knip": "knip" }, "devDependencies": { "@types/node": "^20.14.8", "knip": "^5.30.1", "typescript": "^5.5.4" } } ``` -------------------------------- ### Example Workspace Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration-hints.md An example of a `knip.json` configuration for a monorepo, demonstrating how to define `entry` and `project` patterns within a workspace. ```json { "workspaces": { "packages/app": { "entry": ["src/App.tsx"], "project": ["src/**/*.ts"] } } } ``` -------------------------------- ### GitHub Actions Workflow for Linting Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/using-knip-in-ci.md Example workflow configuration for GitHub Actions to run Knip as part of the linting process. This setup checks out code, sets up Node.js, installs dependencies, and executes the Knip command. ```yaml name: Lint project on: push jobs: lint: runs-on: ubuntu-latest name: Ubuntu/Node v24 steps: - uses: actions/checkout@v6 - uses: actions/setup-node@v6 with: node-version: 24 - name: Install dependencies run: npm install --ignore-scripts - name: Run knip run: npm run knip ``` -------------------------------- ### v1 Configuration with Cypress Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/migration-to-v1.md An example of a v1 configuration including specific settings for Cypress. ```json { "entry": "src/index.ts!", "project": "src/**/*.ts!", "cypress": { "entry": "src/**/*.e2e.ts" } } ``` -------------------------------- ### Install Dependencies with Bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Use this command to install project dependencies after updating `package.json`. ```shell bun install ``` -------------------------------- ### Example of package.json with a script referencing a binary Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/handling-issues.mdx This JSON example shows a package.json configuration where a script ('commitlint') relies on a binary ('commitlint'). Knip correctly identifies that the '@commitlint/cli' package provides this binary. ```json { "name": "lib", "scripts": { "commitlint": "commitlint --edit" }, "devDependencies": { "@commitlint/cli": "*" } } ``` -------------------------------- ### v1 Configuration Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/migration-to-v1.md The equivalent configuration object for v1. ```json { "entry": ["src/index.ts!"], "project": ["src/**/*.ts!"] } ``` -------------------------------- ### Example .nycrc.json Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/writing-a-plugin/index.md An example configuration file for the 'nyc' plugin, showing how to extend default configurations and enable coverage checks. ```json { "extends": "@istanbuljs/nyc-config-typescript", "check-coverage": true } ``` -------------------------------- ### Install Dependencies with pnpm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Use this command to install project dependencies after updating `package.json`. ```shell pnpm install ``` -------------------------------- ### Install Unlisted Package with Bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Install packages that Knip reports as unlisted or missing using Bun. ```shell bun add unlisted-package ``` -------------------------------- ### Install Dependencies with npm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Use this command to install project dependencies after updating `package.json`. ```shell npm install ``` -------------------------------- ### Run Knip without installation using bunx Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Execute Knip directly using bunx without a local installation. Requires TypeScript and @types/node to be installed. ```shell bunx knip ``` -------------------------------- ### Example tsconfig.json Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/faq.md This tsconfig.json example shows how to extend a base configuration and set a JSX import source. Knip uses this to identify dependencies like @tsconfig/node20 and hastscript. ```json { "extends": "@tsconfig/node20/tsconfig.json", "compilerOptions": { "jsxImportSource": "hastscript/svg" } } ``` -------------------------------- ### Install Dependencies with Yarn Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Use this command to install project dependencies after updating `package.json`. ```shell yarn ``` -------------------------------- ### Move Top-Level Entry/Project to Workspaces Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration-hints.md This configuration example shows how to move top-level `entry` and `project` keys to the appropriate workspace configuration, as they are ignored in monorepos. ```jsonc { "entry": ["src/App.tsx"], // move entry/project from here... ↴ "project": ["src/**/*.ts"], "workspaces": { ".": { "entry": ["src/App.tsx"], // ...to the correct workspace(s) ↲ "project": ["src/**/*.ts"], }, }, } ``` -------------------------------- ### Run Knip without installation using pnpm dlx Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Execute Knip directly using pnpm dlx without a local installation. Requires TypeScript and @types/node to be installed. ```shell pnpm dlx knip ``` -------------------------------- ### Start MCP Server Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/integrations.md Use this command to start the standalone MCP Server. The VS Code extension includes this server built-in. ```sh npx @knip/mcp ``` -------------------------------- ### Vite 5 App Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/knip/fixtures/plugins/vite5/index.html Example of a Vite 5 application's main entry point. It shows how to import modules and log environment variables. ```javascript // import '/src/commented-out.ts' import '/src/main.tsx' console.log(import.meta.env.MODE, '/src/decoy.ts') ``` -------------------------------- ### Install Unlisted Package with pnpm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Install packages that Knip reports as unlisted or missing using pnpm. ```shell pnpm add unlisted-package ``` -------------------------------- ### Playwright Configuration Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/explanations/plugins.md This example shows a typical Playwright configuration file. Knip's Playwright plugin reads `testDir` and `testMatch` to determine entry patterns. ```typescript import type { PlaywrightTestConfig } from '@playwright/test'; const config: PlaywrightTestConfig = { testDir: 'integration', testMatch: ['**/*-test.ts'], }; export default config; ``` -------------------------------- ### Install Knip Dependencies with bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Manually install Knip, TypeScript, and Node types using bun. These are peer dependencies for compatibility. ```shell bun add -D knip typescript @types/node ``` -------------------------------- ### Run Knip with Bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/cli.md Execute Knip using the Bun runtime instead of Node.js. Requires Bun to be installed. ```shell knip-bun ``` -------------------------------- ### Install Development Dependencies Source: https://github.com/webpro-nl/knip/blob/main/templates/vscode-nested-root/README.md This script installs the necessary development dependencies for testing the VS Code Knip extension with a nested project structure. ```sh packages/vscode-knip/scripts/dev-install.sh ``` -------------------------------- ### Install Unlisted Package with npm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Install packages that Knip reports as unlisted or missing using npm. ```shell npm install unlisted-package ``` -------------------------------- ### package.json with Entry Files and Scripts Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/script-parser.md This JSON example demonstrates how `package.json` fields like `main`, `exports`, `bin`, and `scripts` are used to define entry files and executable scripts. Knip parses these to identify all referenced files. ```json { "name": "my-package", "main": "index.js", "exports": { "./lib": { "import": "./dist/index.mjs", "require": "./dist/index.cjs" } }, "bin": { "program": "bin/cli.js" }, "scripts": { "build": "rollup src/entry.ts", "start": "node --loader tsx server.ts" } } ``` -------------------------------- ### Install Unlisted Package with Yarn Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/auto-fix.mdx Install packages that Knip reports as unlisted or missing using Yarn. ```shell yarn add unlisted-package ``` -------------------------------- ### Tailwind Plugin Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/writing-a-plugin/index.md This example demonstrates a basic Knip plugin that defines entry file patterns. It includes title, enablers, and an isEnabled function to check for the 'tailwindcss' dependency. ```typescript import type { IsPluginEnabled, Plugin } from '../../types/config.js'; import { hasDependency } from '../../util/plugin.js'; const title = 'Tailwind'; const enablers = ['tailwindcss']; const isEnabled: IsPluginEnabled = ({ dependencies }) => hasDependency(dependencies, enablers); const entry = ['tailwind.config.{js,cjs,mjs,ts}']; const plugin: Plugin = { title, enablers, isEnabled, entry, }; export default plugin; ``` -------------------------------- ### Create a New Plugin using create-plugin Script Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/writing-a-plugin/index.md Use the `create-plugin` script to quickly scaffold a new plugin. This script automates the setup of source files, test files, and fixtures. ```shell cd packages/knip pnpm create-plugin --name tool ``` -------------------------------- ### Namespace Usage Examples (All Exports Used) Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/namespace-imports.md Demonstrates various ways to use a namespace import where all its exports are considered used. ```typescript import * as NS from './my-namespace.js'; import send from 'stats'; send(NS); const spread = { ...NS }; const shorthand = { NS }; const assignment = NS; const item = [NS]; type TypeOf = typeof NS; Object.values(NS); for (const fruit in NS) { // } export { NS }; export { NS as AliasedNS }; export = NS; ``` -------------------------------- ### Example: Dynamic Component Loading with @loadable/component Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/handling-issues.mdx This example demonstrates how Knip handles dynamic imports for components using '@loadable/component'. It shows a scenario where one export is directly used and another is accessed via a library function, illustrating potential challenges in tracking usage. ```typescript import loadable from '@loadable/component'; export const DynamicApple = dynamic(() => import('./components.js').then(mod => mod.Apple) ); export const LoadableOrange = loadable(() => import('./components.js'), { resolveComponent: components => components.Orange, }); ``` ```typescript export const Apple = () => 'Apple'; export const Orange = () => 'Orange'; ``` -------------------------------- ### package.json Scripts for Dependencies and Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/script-parser.md This JSON example shows `package.json` scripts where CLI arguments like `--import`, `--loader`, and configuration file flags are used. Knip identifies dependencies and configuration files based on these arguments. ```json { "name": "my-lib", "scripts": { "start": "node --import tsx/esm run.ts", "bundle": "tsup -c tsup.lib.config.ts", "type-check": "tsc -p tsconfig.app.json" } } ``` -------------------------------- ### Knip Output Example Source: https://github.com/webpro-nl/knip/blob/main/templates/playground/monorepo/README.md Example output from running `npm run knip` in the monorepo, showing detected issues like unused and unlisted dependencies, and unused exports. ```bash $ npm run knip Unused dependencies (1) tinyglobby package.json Unlisted dependencies (1) js-yaml packages/shared/src/used-fn.ts Unused exports (1) unusedFunction unknown packages/shared/src/exports.ts:7:14 ``` -------------------------------- ### Install Knip Dependencies with npm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Manually install Knip, TypeScript, and Node types using npm. These are peer dependencies for compatibility. ```shell npm install -D knip typescript @types/node ``` -------------------------------- ### Install Knip Dependencies with pnpm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Manually install Knip, TypeScript, and Node types using pnpm. These are peer dependencies for compatibility. ```shell pnpm add -D knip typescript @types/node ``` -------------------------------- ### Install Knip Dependencies with yarn Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Manually install Knip, TypeScript, and Node types using yarn. These are peer dependencies for compatibility. ```shell yarn add -D knip typescript @types/node ``` -------------------------------- ### Vitest Configuration Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/explanations/plugins.md This TypeScript configuration for Vitest sets up testing environment and coverage provider. Knip parses this to identify dependencies such as `@vitest/coverage-istanbul` and `vitest-environment-happy-dom`. ```typescript import { defineConfig } from 'vitest/config'; export default defineConfig({ test: { coverage: { provider: 'istanbul', }, environment: 'happy-dom', }, }); ``` -------------------------------- ### Install Knip v3 with bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v3.mdx Command to install Knip v3 as a dev dependency using bun. ```shell bun add -D knip ``` -------------------------------- ### Using a custom configuration file path Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/configuration.md Example of using the `--config` flag to specify a custom configuration file path. ```sh knip --config path/to/knip.json ``` -------------------------------- ### ESLint Configuration Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/explanations/plugins.md This JSON configuration for ESLint specifies extended configurations and plugins. Knip parses this to identify dependencies like `eslint-config-airbnb`, `eslint-config-prettier`, and `@typescript-eslint/eslint-plugin`. ```json { "extends": ["airbnb", "prettier"], "plugins": ["@typescript-eslint"] } ``` -------------------------------- ### package.json Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/state-of-knip.md Example of a script in package.json that uses Yarn to run Vitest with a specific configuration file. ```json { "scripts": { "test": "yarn --cwd packages/frontend vitest -c vitest.components.config.ts" } } ``` -------------------------------- ### Example of using experimental tags in CLI Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v4.mdx Examples of using the experimental tags option in the CLI to include or exclude specific tagged exports. ```shell knip --experimental-tags=+custom ``` ```shell knip --experimental-tags=-custom,-internal ``` -------------------------------- ### Install Knip v6 Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v6.md Upgrade to the latest version of Knip by running this command in your project's development dependencies. ```sh npm install -D knip@latest ``` -------------------------------- ### Vite Configuration with TypeScript Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/explanations/plugins.md Example of a Vite configuration file written in TypeScript. This snippet shows how to import Vite and React plugins, and configure testing environments. Knip dynamically loads and parses such files, also treating them as entry points for static analysis. ```typescript import { defineConfig } from 'vite'; import react from '@vitejs/plugin-react'; export default defineConfig(async ({ mode, command }) => { return { plugins: [react()], test: { setupFiles: ['./setup-tests.ts'], environment: 'happy-dom', coverage: { provider: 'c8', }, }, }; }); ``` -------------------------------- ### knip.js Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v5.mdx Example of exports in a file that will be checked by Knip. ```typescript export const version = 'v5'; export const getRocket = () => '🚀'; ``` -------------------------------- ### index.js Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v5.mdx Example of using named imports instead of namespaced imports. ```typescript import { version, getRocket } from './knip.js'; send({ version, getRocket }); ``` -------------------------------- ### Knip Output Example Source: https://github.com/webpro-nl/knip/blob/main/templates/playground/basic/README.md This snippet shows the expected output when running `npm run knip` on the provided codebase, highlighting the types of issues Knip detects. ```bash $ npm run knip Unused files (1) clutter.ts Unlisted dependencies (1) lodash util.ts Unused exports (1) unusedFunction unknown util.ts:6:14 ``` -------------------------------- ### Requesting a File Descriptor Source: https://github.com/webpro-nl/knip/blob/main/packages/language-server/README.md Example of how a client requests a file descriptor to get available data for a document using the REQUEST_FILE_NODE request. ```typescript const file = await this.#client.sendRequest(REQUEST_FILE_NODE, { uri: editor.document.uri.toString(), }); ``` -------------------------------- ### Requesting the package.json File Descriptor Source: https://github.com/webpro-nl/knip/blob/main/packages/language-server/README.md Example of how a client requests the package.json file descriptor to get dependency usage data using the REQUEST_PACKAGE_JSON request. ```typescript const packageJson = await this.#client.sendRequest(REQUEST_PACKAGE_JSON); ``` -------------------------------- ### Import and Use a UI Component in MDX Source: https://github.com/webpro-nl/knip/blob/main/packages/knip/fixtures/plugins/astro/src/content/blog/using-mdx.mdx This example demonstrates how to import and use a UI component within an MDX file. When rendered, it should display a clickable button. ```mdx import HeaderLink from '../../components/HeaderLink.astro'; Embedded component in MDX ``` -------------------------------- ### Custom knip.json configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/configuration.md An example custom configuration to include `.js` files in the `scripts` folder. ```json { "$schema": "https://unpkg.com/knip@6/schema.json", "entry": ["src/index.ts", "scripts/{build,create}.js"], "project": ["src/**/*.ts", "scripts/**/*.js"] } ``` -------------------------------- ### Run Knip without installation using npx Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Execute Knip directly using npx without a local installation. Requires TypeScript and @types/node to be installed. ```shell npx knip ``` -------------------------------- ### TypeScript Static Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/dynamic-configuration.mdx Use a TypeScript file to define a static Knip configuration with type annotations. Ensure 'knip' is installed for type checking. ```typescript import type { KnipConfig } from 'knip'; const config: KnipConfig = { entry: ['src/index.ts'], project: ['src/**/*.ts'], }; export default config; ``` -------------------------------- ### Namespace Import and Export Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/slim-down-to-speed-up.md An example showing how Knip previously used `findReferences` to trace usage of an exported member within a namespace. ```typescript export const referencedExport = () => {}; ``` ```typescript import * as NS from './namespace.ts'; NS.referencedExport(); ``` -------------------------------- ### Initialize Knip Configuration with npm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Use this command to initialize Knip configuration with npm. ```shell npm init @knip/config ``` -------------------------------- ### Initialize Knip Configuration with bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Use this command to initialize Knip configuration with bun. ```shell bun create @knip/config ``` -------------------------------- ### Astro Configuration with Starlight Integration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/writing-a-plugin/index.md Example of an Astro configuration file using the Starlight integration, which defines custom components. This is relevant for understanding how custom configurations might affect plugin integration. ```typescript import starlight from '@astrojs/starlight'; import { defineConfig } from 'astro/config'; export default defineConfig({ integrations: [ starlight({ components: { Head: './src/components/Head.astro', Footer: './src/components/Footer.astro', }, }), ], }); ``` -------------------------------- ### Referencing Catalog Entries in package.json Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/catalogs.md Example of how to reference catalog entries using the `catalog:` protocol in a workspace's `package.json`. `catalog:` refers to the default catalog, while `catalog:name` refers to a named catalog. ```json { "dependencies": { "react": "catalog:", "zod": "catalog:validation" } } ``` -------------------------------- ### Remote Configuration Import Source: https://github.com/webpro-nl/knip/blob/main/packages/knip/fixtures/plugins/astro/src/pages/about/mdx-with-layout.mdx This example shows how to import a remote JSON configuration file in Astro using dynamic import with a specified type. ```javascript export const loadConfig = () => import('remote-config/settings.json', { with: { type: 'json' } }); ``` -------------------------------- ### Create Knip Configuration File Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration-hints.md When a project is unconfigured, Knip suggests creating a `knip.json` file and adding entry/project patterns. ```text Create knip.json configuration file, and add entry and/or refine project files (42 unused files) ``` -------------------------------- ### Tagging Exports Example (TS) Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/cli.md Example of how exports can be tagged with JSDoc/TSDoc tags for filtering. ```ts /** * Description of my exported value * * @type number * @internal Important matters * @lintignore */ export const myExport = 1; ``` -------------------------------- ### Combine Workspace Filters for Inclusion and Exclusion Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/monorepos-and-workspaces.md Combine multiple `--workspace` arguments to include specific workspaces and exclude others. This example includes all workspaces under '@myorg/' except '@myorg/legacy'. ```sh knip --workspace @myorg/* --workspace '!@myorg/legacy' knip --workspace './apps/*' --workspace '@shared/utils' ``` -------------------------------- ### Extensionless Imports Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/handling-issues.mdx Knip may not resolve extensionless imports for non-standard extensions like `.svg`. Ensure file extensions are included for imports in hosts supporting ES Modules. ```typescript import Component from './Component'; // → ✅ Should resolve to ./Component.vue import ArrowIcon from '../icons/Arrow'; // → ❌ Does NOT resolve to ../icons/Arrow.svg ``` -------------------------------- ### Add Entry/Project Files Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration-hints.md If a configuration file exists but is incomplete, Knip suggests adding or refining `entry` and `project` patterns. ```text Add entry and/or refine project files (42 unused files) ``` -------------------------------- ### Initialize Knip Configuration with pnpm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Use this command to initialize Knip configuration with pnpm. ```shell pnpm create @knip/config ``` -------------------------------- ### Initialize Knip Configuration with yarn Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Use this command to initialize Knip configuration with yarn. ```shell yarn create @knip/config ``` -------------------------------- ### Reproduce StackBlitz Project Source: https://github.com/webpro-nl/knip/blob/main/AGENTS.md Use this command to fetch the source files of a StackBlitz project for reproduction purposes. ```sh pnpx stackblitz-zip https://stackblitz.com/edit/{name} {filename}.zip ``` -------------------------------- ### Command Line Usage for Local Reporter Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/reporters.md Demonstrates how to execute Knip and specify a local custom reporter file using the `--reporter` argument. ```bash knip --reporter ./my-reporter.ts ``` -------------------------------- ### Install Knip v3 with yarn Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v3.mdx Command to install Knip v3 as a dev dependency using yarn. ```shell yarn add -D knip ``` -------------------------------- ### Configure Project Files Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration.md Specify project files using an array of glob patterns. ```json { "project": ["src/**/*.ts", "scripts/**/*.ts"] } ``` -------------------------------- ### Install Knip v3 with pnpm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v3.mdx Command to install Knip v3 as a dev dependency using pnpm. ```shell pnpm add -D knip ``` -------------------------------- ### Run Knip with bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Execute this command to run Knip and lint your project using bun. ```shell bun knip ``` -------------------------------- ### Install Knip v3 with npm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v3.mdx Command to install Knip v3 as a dev dependency using npm. ```shell npm install -D knip ``` -------------------------------- ### Run Knip with Compact Reporter Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/reporters.md Use the `compact` reporter for a concise overview of findings. This is a quick way to see the main issues. ```sh knip --reporter compact ``` -------------------------------- ### Treat Configuration Hints as Errors Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration.md Configure Knip to exit with a non-zero code if any configuration hints are found. ```json { "treatConfigHintsAsErrors": true } ``` -------------------------------- ### Markdown Report Example Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/reporters.md This is an example of the output generated by the built-in Markdown reporter, showcasing how issues are presented in tables under different headings. ```markdown # Knip report ## Unused files (1) - src/unused.ts ## Unlisted dependencies (2) | Name | Location | Severity | | :-------------- | :---------------- | :------- | | unresolved | src/index.ts:8:23 | error | | @org/unresolved | src/index.ts:9:23 | error | ## Unresolved imports (1) | Name | Location | Severity | | :----------- | :----------------- | :------- | | ./unresolved | src/index.ts:10:12 | error | ``` -------------------------------- ### Example of tagged exports in JSDoc Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v4.mdx An example demonstrating how to tag exports in JSDoc comments for selective reporting. ```typescript /** @custom */ export const myExport = 1; ``` -------------------------------- ### Run Knip with npm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Execute this command to run Knip and lint your project using npm. ```shell npm run knip ``` -------------------------------- ### Configure Entry Files Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration.md Specify entry files using an array of glob patterns. Use '!' to negate patterns. ```json { "entry": ["src/index.ts", "scripts/*.ts", "!scripts/except-this-one.ts"] } ``` -------------------------------- ### Example Custom Preprocessor Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/features/reporters.md A basic example of a custom preprocessor written in TypeScript. It imports the Preprocessor type and exports a function that returns the provided options, serving as a template for modifications. ```typescript import type { Preprocessor } from 'knip'; const preprocess: Preprocessor = function (options) { // modify options.issues and options.counters return options; }; export default preprocess; ``` -------------------------------- ### index.js Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v5.mdx Example of a namespaced import where only the namespace itself is referenced. ```typescript import * as NS from './knip.js'; import send from 'stats'; send(NS); ``` -------------------------------- ### Configure Workspaces in knip.json Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration.md Define entry points and project files for different workspaces. The root workspace is represented by ".". ```json { "workspaces": { ".": { "entry": ["scripts/*.ts"], "project": ["scripts/**/*.ts"] }, "packages/*": { "entry": ["src/index.ts"], "project": ["src/**/*.ts"] } } } ``` -------------------------------- ### Define Namespace Exports Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/namespace-imports.md Defines two constants, `start` and `end`, for namespace usage. ```typescript export const start = 1; export const end = 1; ``` -------------------------------- ### Run Knip with pnpm Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/overview/getting-started.mdx Execute this command to run Knip and lint your project using pnpm. ```shell pnpm knip ``` -------------------------------- ### index.js Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v5.mdx Example of a namespaced import referencing one of the exports explicitly. ```typescript import * as NS from './knip.js'; console.log(NS.version); ``` -------------------------------- ### Run Plugin Tests with Bun Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/writing-a-plugin/index.md Execute the tests for your newly created plugin using the Bun test runner. ```shell bun test test/plugins/tool.test.ts ``` -------------------------------- ### MCP Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/mcp-server/README.md Example of how to add the Knip MCP Server to your MCP configuration. ```json { // or "mcpServers" "servers": { "knip": { "command": "npx", "args": ["-y", "@knip/mcp"], }, }, } ``` -------------------------------- ### knip.js Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v5.mdx Example of using an arbitrary JSDoc tag to exclude an export from reports. ```typescript export const version = 'v5'; /** @launch */ export const getRocket = () => '🚀'; ``` -------------------------------- ### Run Smoke Tests with Bun Source: https://github.com/webpro-nl/knip/blob/main/AGENTS.md Execute smoke tests using the Bun runtime. Ensure you are in the correct package directory. ```sh cd packages/knip pnpm test --runtime bun --smoke ``` -------------------------------- ### knip.js Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/knip-v5.mdx Example of using the @public JSDoc tag to exclude an export from reports. ```typescript export const version = 'v5'; /** @public */ export const getRocket = () => '🚀'; ``` -------------------------------- ### Enable Performance Metrics Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/performance.md Use the `--performance` flag to view the invocation count and time spent in potentially expensive functions during Knip analysis. This helps identify performance bottlenecks. ```sh knip --performance ``` -------------------------------- ### Build Core Package and Run All Tests Source: https://github.com/webpro-nl/knip/blob/main/AGENTS.md Build the core package and then run all tests. This is typically done after changes to auto-fix, formatting, or reporter functionality. ```sh cd packages/knip pnpm build pnpm test ``` -------------------------------- ### Create Workspace Configuration Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/configuration-hints.md For unconfigured monorepos, Knip recommends creating a `knip.json` file with a `workspaces` object, specifying the workspace path. ```text Create knip.json configuration file with workspaces["packages/app"] object (42 unused files) ``` -------------------------------- ### Export/Import Match (Easy to Find) Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/blog/slim-down-to-speed-up.md An example of a simple export/import match that Knip can easily identify. ```typescript import { MyThing } from './thing.ts'; ``` ```typescript export const MyThing = 'cool'; ``` -------------------------------- ### Dependencies Shortcut Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/reference/cli.md A shortcut to include all types of dependency-related issues. ```sh --include dependencies,unlisted,binaries,unresolved,catalog ``` -------------------------------- ### Namespace Usage Example (Property Access) Source: https://github.com/webpro-nl/knip/blob/main/packages/docs/src/content/docs/guides/namespace-imports.md Shows a case where accessing a namespace property causes other exports to be considered unused. ```javascript import * as NS from './namespace.js'; const begin = NS.start; send(NS); ```