|
| 1 | +--- |
| 2 | +title: How Knip works |
| 3 | +description: The mental model behind Knip. How entry files, the module graph and reachability decide what counts as unused, and why surprising results trace back to them. |
| 4 | +--- |
| 5 | + |
| 6 | +Knip finds and removes unused code and dependencies. It starts at your entry |
| 7 | +files, follows every import to the files they reach, and builds a graph of |
| 8 | +everything connected. Anything left out is reported as unused: a file no entry |
| 9 | +reaches, an export no file imports, a dependency no file uses. |
| 10 | + |
| 11 | +This page explains how Knip produces its results. |
| 12 | + |
| 13 | +## Build and analyze |
| 14 | + |
| 15 | +Knip runs in two phases. |
| 16 | + |
| 17 | +The build phase starts from the entry files. Knip reads a file, parses it, |
| 18 | +collects its imports and exports, resolves each import to another file, and |
| 19 | +repeats for every newly reached file. The result is a graph of the project: |
| 20 | +every reachable file, what it imports, and what imports it. |
| 21 | + |
| 22 | +Resolution is broad by design. Knip follows custom path aliases and extensions, |
| 23 | +reads references out of config files and [shell scripts][1], and "[compiles][2]" |
| 24 | +non-standard files like `.vue` and `.svelte` so their imports are in the graph |
| 25 | +too. |
| 26 | + |
| 27 | +The analysis phase queries that graph. It never re-parses anything. It walks the |
| 28 | +edges already recorded: |
| 29 | + |
| 30 | +- a **file** is unused when no entry reaches it |
| 31 | +- an **export** is unused when no other file imports it |
| 32 | +- a **dependency** is unused when no file imports it (and _unlisted_ when a file |
| 33 | + imports it but `package.json` doesn't list it) |
| 34 | + |
| 35 | +Analysis happens by quering the graph. Your results are only as accurate as the |
| 36 | +graph is complete. And the graph depends on your entry files. |
| 37 | + |
| 38 | +## Entry files decide what Knip can see |
| 39 | + |
| 40 | +The graph contains only what's reachable from an entry file. Miss one entry, and |
| 41 | +everything reachable _only_ from it looks unused. |
| 42 | + |
| 43 | +Ideally you don't need to list entries by hand. Knip starts from sensible |
| 44 | +[defaults][3] like `src/index.ts`, and [plugins][4] automatically add the entry |
| 45 | +points specific to your frameworks and libraries. The Vitest plugin adds your |
| 46 | +test files, the Astro plugin adds your pages, and so on. This is why plugins are |
| 47 | +important: each one tells Knip about a whole class of files it might otherwise |
| 48 | +never reach. |
| 49 | + |
| 50 | +## Findings come in chains |
| 51 | + |
| 52 | +Unused results cascade. They don't appear in isolation. Say a tool generates an |
| 53 | +entry file Knip doesn't know about: |
| 54 | + |
| 55 | +- that file looks unused, because nothing reaches it |
| 56 | +- every file it imports looks unused too, because nothing else reaches them |
| 57 | +- every export in those files looks unused |
| 58 | +- every dependency only those files used looks unused |
| 59 | + |
| 60 | +One unreached entry can turn into dozens of findings. The chain also runs in |
| 61 | +reverse: add the entry, and the whole cascade collapses at once. |
| 62 | + |
| 63 | +So read the output from the root down: **unused files first, then exports, then |
| 64 | +dependencies.** A single unused file usually explains a long list of results |
| 65 | +below it. Fix causes at the root and the rest disappears. |
| 66 | + |
| 67 | +## A surprising result might be a gap, not a bug |
| 68 | + |
| 69 | +When Knip flags something you're sure is used, it's based on the graph: it |
| 70 | +couldn't reach that code from an entry. The cause is usually one of: |
| 71 | + |
| 72 | +- a missing entry for a tool or convention Knip doesn't know yet |
| 73 | +- a dynamic import Knip can't follow statically, or a path it can't resolve |
| 74 | +- a transitive dependency of a package that resolves only because something else |
| 75 | + installs it |
| 76 | + |
| 77 | +In each case the fix is to teach Knip: add the entry, configure the plugin, list |
| 78 | +the dependency. Don't hide the result. [Configuring project files][5] and |
| 79 | +[resolving reported issues][6] cover how, issue type by issue type. |
| 80 | + |
| 81 | +[1]: ../features/script-parser.md |
| 82 | +[2]: ../features/compilers.md |
| 83 | +[3]: ./entry-files.md |
| 84 | +[4]: ./plugins.md |
| 85 | +[5]: ../guides/configuring-project-files.md |
| 86 | +[6]: ../guides/handling-issues.mdx |
0 commit comments