How to check unused npm packages?
Tags: node, npm, javascript
Published: 2021-05-16 15:49:20

---
title: How to check unused npm packages?
published: true
date: 2021-02-26 00:00:00 UTC
tags: node, npm, javascript
canonical_url: https://www.mariokandut.com/how-to-check-unused-npm-packages/
---

When building an application it is very common to install various npm modules, the `package.json` and node-modules folder grow, so does the code for the application. It is best practice minimizing the code you have to maintain. A first step would be to remove unused code. Let's start with removing unused npm modules.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#how-to-remove-unused-npm-packages)How to remove unused npm packages

There are several solutions available, `depcheck` and `npm-check` are the most common ones.

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#depcheck)depcheck

[Depcheck](https://www.npmjs.com/package/depcheck) analyzes the dependencies in a project to see: how each dependency is used, which dependencies are useless, and which dependencies are missing from package.json.

To use `depcheck` from the command line you have to install it. `depcheck` requires Node.js >= 10.

```
npm install -g depcheck
```

After installing it, it can be used with typing `depcheck` in the root project directory, where the `package.json` file is. The full syntax of the command looks like this `depcheck [directory] [arguments]`. Depending on the size of your project the execution can take a while.

Your output should look something like this.

![depcheck sample code](https://www.mariokandut.com/static/806f6dca4e50f94bc1778e7237393744/96e35/npm-depcheck.png "depcheck sample code")

I ran `depcheck` in the repository of this website. The output shows that I have six unused dependencies (3x dependencies, 3x dev dependencies), which I am going to remove with `npm uninstall`.

If you don't want to install `depcheck` globally, run it with `npx`.

```
npx depcheck
```

[💰: $100 (credits) for you to start your cloud journey with DigitalOcean!](https://m.do.co/c/29f238135b61)

You can also pass additional arguments to `depcheck`, please have a look at the [official documentation](https://www.npmjs.com/package/depcheck).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#npm-check)npm-check

[npm-check](https://www.npmjs.com/package/npm-check) checks for outdated, incorrect, and unused dependencies.

To use `npm-check` from the command line you have to install it. It requires Node >= 0.11.

```
npm install -g npm-check
```

After installing it, it can be used with typing `npm-check` in the root project directory, where the `package.json` file is. Depending on the size of your project the execution can take a while.

The output of `npm-check` has more information compared to `depcheck`.

![npm-check sample](https://www.mariokandut.com/static/3160c08dd06dbff908d187c67d5c1536/fcda8/npm-npm-check.png "npm-check sample")

I ran `npm-check` in the repository of this website, and the output is quite long, since I have not updated to the latest major version of gatsby (It's on the todo list.).`npm-check` will give you a nice and clear output of the out-of-date dependencies and unused dependencies. It also has a nice, interactive dependency update feature, when adding the `-u`or `--update` flag.`npm-check` will then show an interactive UI for choosing which modules to update and automatically updates versions referenced in the package.json. Have a look at the [official documentation](https://www.npmjs.com/package/npm-check) for a full list of options when using `npm-check`.

If you don't want to install `npm-check` globally, run it with `npx`.

```
npx npm-check
```

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#tldr)TL;DR

- Best practice is to remove unused code.
- Use tools like `depcheck` or `npm-check` to find unused dependencies.

Thanks for reading and if you have any **questions** , use the **comment** function or **send me a message** **[@mariokandut](https://www.twitter.com/mariokandut)**.

If you want to know more about **[Node](https://www.mariokandut.com/tags/node)**, have a look at these [Node Tutorials](https://www.mariokandut.com/tags/node).

### [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#references-and-big-thanks)References (and Big thanks):

[npm depcheck](https://www.npmjs.com/package/depcheck), [Sameer](https://medium.com/fuzzy-code/how-to-check-unused-npm-packages-977d06f0f15a), [npm-check](https://www.npmjs.com/package/npm-check), [fam](https://javascript.plainenglish.io/how-to-check-if-you-have-unused-dependencies-in-your-javascript-app-2c69dd97c49e)