An Automatic Instrument For Discovering Unused Recordsdata, Exports, And Dependencies — Smashing Mag

Let’s face it. Maximum folks want developing new options and person interfaces over repairs duties similar to code cleanup, venture configuration, and dependency control.

Loads of the uninteresting and repetitive issues, like formatting and linting, are most commonly solved issues of gear like Prettier, ESLint, and TypeScript.

But there’s any other house that frequently doesn’t obtain a lot consideration: dealing with unused recordsdata, exports, and dependencies. That is very true as tasks develop over the years, negatively impacting maintainability in addition to our enthusiasm for it. Those unused artifacts frequently pass overlooked as a result of they’re most often demanding to seek out.

The place do you get started in search of unused issues? I guess you’ve achieved a world seek for a dependency to determine whether or not it’s nonetheless used. Do you know you’ll right-click a record and “In finding Report References” in VS Code? Those are the stuff you shouldn’t must do. They’re tedious duties that may — and must — be computerized.

There’s Were given to Be a Higher Manner

When I used to be in an increasing codebase, the selection of unused recordsdata and exports in it saved rising. Monitoring them changed into increasingly more tricky, so I began in search of computerized answers to lend a hand.

I used to be satisfied I discovered some current gear, similar to ts-prune. After some preliminary runs, I
came upon that our codebase required numerous configuration and nonetheless produced many false positives. I researched how different codebases attempt to keep tidy and discovered there’s an enormous alternative on this area.

Alongside the street, I additionally discovered depcheck, which offers with numerous complexity and customizations in JavaScript tasks. After which there’s unimported, which additionally does a notable task of discovering dangling recordsdata and unused dependencies.

However none of those gear treated my venture rather well. The truth that I must use a mix of them wouldn’t be a showstopper, however I couldn’t get the configurations correct for dealing with the venture’s customizations with out reporting too many false positives or ignoring an excessive amount of of the venture and leaving a big blind spot.

After all, gear on this house are solely used when they’re absolutely computerized and in a position to hide the entire venture. It additionally didn’t lend a hand that not one of the current gear strengthen monorepos, a construction for repositories that has not too long ago won standard recognition. Closing however no longer least, each ts-prune and depcheck are in repairs mode, that means they’d most probably by no means strengthen monorepos.

Operating Against A Answer

I’m motivated to automate issues and stay tasks in cast form.

Nice developer revel in (DX) helps to keep builders satisfied and productive.

So I began growing an inner device for my venture to look if I may just do higher. It began as an inner script that treated solely the specifics of that specific repository, and right through the adventure, I saved understanding what a blessing and a curse that is. Automating uninteresting stuff is a profitable thought, however getting it correct is this sort of tricky problem — however person who I used to be prepared to take a look at.

Alongside the street, I additionally were given increasingly more satisfied {that a} unmarried device to seek out all classes of unused issues used to be a good suggestion: each and every of them calls for studying and parsing supply recordsdata, and because this can be a slightly pricey process, I feel the environment friendly trail is to trace them in a single pass.

Introducing Knip

So, what’s Knip? I feel it’s best possible classified as a venture linter. It alternatives up the place ESLint ends. The place ESLint handles particular person recordsdata, Knip lints the repository as a complete. It connects all of the dots — on the subject of recordsdata, imports, exports, and dependencies — and reviews what’s unused.

More or less talking, there are two tactics to have a look at Knip. In greenfield tasks, it’s nice to put in Knip and let it develop with the venture. Stay the repository tidy and run Knip manually or in an automatic steady integration (CI) atmosphere.

Differently to have a look at Knip is in greater tasks. Knip is a smart better half for home tasks so far as figuring out unused recordsdata, exports, and dependencies. There is also false positives first of all, but it surely’s a lot more uncomplicated to skip them in comparison to discovering needles in a haystack by yourself.

Moreover, all the way through or after massive refactoring efforts, Knip could be a nice assistant for cleansing issues up. It’s solely human to leave out or overlook issues which might be now not used, much more so when the issues don’t seem to be with regards to the refactoring paintings.

Knip works with conventional JavaScript and trendy TypeScript, has integrated strengthen for monorepos, works with any package deal supervisor, and has many options, small and big, that will let you take care of your tasks.

How Knip Works

Knip begins with a number of access recordsdata and calculates the dependency tree, serving to it know all of the recordsdata which might be used whilst marking the rest recordsdata as unused.

In the meantime, Knip helps to keep monitor of imported exterior dependencies and compares them towards the dependencies in package deal.json to document each unused dependencies and dependencies which might be used however no longer indexed. It additionally helps to keep monitor of inner imports and exports to document unused exports.

Let me provide you with a greater glance below the hood.

Manufacturing Mode

In its default mode, Knip analyzes the entire venture, together with each manufacturing and non-production recordsdata, similar to checks, configurations, Storybook tales, devDependencies, and so forth.

An example run using the default reporter
An instance run the use of the default reporter. (Huge preview)

However this mode may leave out alternatives for trimming. As an example, recordsdata or exports imported solely by way of checks are most often no longer reported as unused. On the other hand, when the export isn’t used any place else, you’ll delete each the export and its checks!

That is why Knip has a manufacturing mode. This mode is extra strict than the default mode, the place Knip will use solely manufacturing code as access recordsdata and solely imagine dependencies (aside from devDependencies).


Many tasks use command line gear that include dependencies. As an example, after putting in ESLint, you’ll use eslint, and Angular makes ng to be had in "scripts" in package deal.json. Knip connects dependencies with binaries and tells you when they’re unused or lacking.

However there’s extra. CI environments, like Azure and GitHub Movements, are configured with YAML recordsdata that may additionally use the similar command line gear.

And in spite of everything, customized scripts might use command line gear by way of spawning kid processes, both the use of local Node.js APIs or with libraries like zx or execa.

Knip has a rising selection of such detections to stay your tasks neat and tidy, refactor after refactor. But what’s so attention-grabbing about the ones scripts? They is also difficult to parse, making it tricky to extract their dependencies. Let’s take a look at an instance:

node -r @scope/package deal/sign up --experimental-loader ts-node/esm/transpile-only ./dir

Right here, we will discover @scope/package deal and ts-node and dir/index.ts are dependencies of this script. Truthfully, that is simply the end of the iceberg. I promise a couple of common expressions received’t be sufficient!

Now, if this script is up to date or got rid of, Knip will let you know if any dependency or record is now not used. However, Knip will even let you know if a dependency is used however no longer indexed explicitly in package deal.json. (You shouldn’t be depending on transitive dependencies anyway, correct?)


There’s an abundance of tooling to be had within the JavaScript ecosystem. And each and every device has its configurations. Some permit YAML or JSON, and a few permit (or require) you to jot down configurations in JavaScript and even TypeScript.

Since there’s no generic strategy to care for the myriad of diversifications, Knip helps plugins. Knip has plugins for gear that can reference dependencies that are supposed to be indexed in package deal.json.

What issues to plugins is how dependencies are referenced. They could be imported in JavaScript like another supply record, and Knip too can parse them as such. But, they’re frequently referenced as strings, a lot the similar as what ESLint does with the extends and plugins choices. Dependencies may also be laid out in implicit tactics, similar to prettier, because of this the eslint-config-prettier dependency. Storybook has the builder: "webpack5" configuration that calls for the @storybook/builder-webpack5 and @storybook/manager-webpack5 dependencies.


Knip parses all forms of JavaScript and TypeScript recordsdata, together with ES modules and CommonJS modules.

However some frameworks paintings with non-standard recordsdata, similar to Vue, Svelte, MDX, and Astro. Knip means that you can configure compilers to incorporate a majority of these recordsdata so they may be able to even be integrated within the research.


Till model 2, Knip used ts-morph to calculate the dependency graph (and a lot more). This labored nice first of all as it abstracted away the TypeScript again finish.

However to strengthen monorepos and compilers whilst keeping up just right efficiency, I noticed I needed to paintings with the TypeScript again finish at once. This required numerous effort, but it surely does supply much more flexibility, in addition to alternatives for extra optimizations. For instance, Knip can traverse the summary syntax tree (AST) of any record solely as soon as to seek out the whole thing it wishes.

Configuration Hints

When Knip reviews a false certain, you’ll configure it to forget about that dependency. Then, when Knip now not reviews the false certain, it’ll document that the configuration may also be up to date, and you’ll take away the unnoticed pieces.


Knip comes with a default reporter and has a couple of further newshounds. There’s a compact reporter, a JSON reporter, and person who makes use of the CODEOWNERS record to turn the code proprietor(s) with each and every reported factor.

Knip additionally means that you can outline a customized reporter. Knip will name your serve as with the result of the research. You’ll then do the rest with it, like writing the effects to a record or sending it to a provider to trace growth over the years.

What’s Subsequent For Knip?

Naturally, I’m no longer achieved operating on Knip. Those are some of the issues I take into account.

Extra Plugins = Much less Configuration

My hope with Knip is that as increasingly more other people begin to use Knip, they’re going to document false positives: one thing is reported as unused however is in use.

A false certain most often has one of the most following 3 reasons:

  • A framework or device is used that Knip does no longer but have a plugin for,
  • The configuration may want growth; as an example, upload an access record that Knip didn’t learn about or forget about one thing with a hard-to-find reference, or
  • Knip has a worm.

As Knip turns into higher with worm fixes and plugins, extra tasks receive advantages as a result of they want much less configuration. Upkeep will turn into extra relaxing and more uncomplicated for everybody!

If you end up the use of Knip and taking part in it, don’t let false positives scare you away, however document them as an alternative. Please supply a reproducible check case, and I’m certain we will paintings it out. Moreover, I’ve written an entire information detailing the best way to write a brand new plugin for Knip.


Similar to ESLint, Knip may have a --fix way to robotically repair all forms of problems. The theory is that this will robotically care for issues similar to:

  • Take away the export key phrase for unused exports,
  • Uninstall unused dependencies and set up unlisted dependencies, and
  • Delete unused recordsdata.

Given sufficient pastime from the neighborhood, I’m excited to begin construction this selection!


Integrations similar to a VS Code plugin or a GitHub Motion sound like cool alternatives. I’m satisfied to collaborate and notice the place we will take it.

Demo Knip

I feel one of the simplest ways to know Knip is to get your arms on it. So, I’ve created a CodeSandbox template that you’ll fork and spin up Knip in a brand new terminal with npm run knip.


Knip objectives that can assist you take care of your JavaScript or TypeScript repositories, and I’m more than happy loads of tasks are already reduce by way of Knip day by day.

There may be loads of room for growth, insects to mend, documentation to strengthen, and plugins so as to add! Your concepts and contributions are completely welcome — and inspired — over at

Smashing Editorial

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Back To Top
Would love your thoughts, please comment.x