How to check unused npm packages?
© https://nodejs.org/en/

How to check unused npm packages?

Minimize your code with removing unused one.

ByMario Kandut

honey pot logo

Europe’s developer-focused job platform

Let companies apply to you

Developer-focused, salary and tech stack upfront.

Just one profile, no job applications!

This article is based on Node v16.15.1 and NPM 8.11.0.

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.

How to remove unused npm packages

💰 The Pragmatic Programmer: journey to mastery. 💰 One of the best books in software development, sold over 200,000 times.

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

depcheck

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

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

You can also pass additional arguments to depcheck, please have a look at the official documentation.

npm-check

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

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 -uor --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 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

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.

If you want to know more about Node, have a look at these Node Tutorials.

References (and Big thanks):

npm depcheck, Sameer, npm-check, fam

More node articles:

Getting started with Webpack

How to list/debug npm packages?

How to specify a Node.js version

How to create a web server in Node.js

How to dynamically load ESM in CJS

How to convert a CJS module to an ESM

How to create a CJS module

How to stream to an HTTP response

How to handle binary data in Node.js?

How to use streams to ETL data?

How to connect streams with pipeline?

How to handle stream errors?

How to connect streams with pipe?

What Is a Node.js Stream?

Handling Errors in Node (asynchronous)

Handling Errors in Node.js (synchronous)

Introduction to errors in Node.js

Callback to promise-based functions

ETL: Load Data to Destination with Node.js

ETL: Transform Data with Node.js

ETL: Extract Data with Node.js

Event Emitters in Node.js

How to set up SSL locally with Node.js?

How to use async/await in Node.js

What is an API proxy?

How to make an API request in Node.js?

How does the Event Loop work in Node.js

How to wait for multiple Promises?

How to organize Node.js code

Understanding Promises in Node.js

How does the Node.js module system work?

Set up and test a .env file in Node

How to Use Environment Variables in Node

How to clean up node modules?

Restart a Node.js app automatically

How to update a Node dependency - NPM?

What are NPM scripts?

How to uninstall npm packages?

How to install npm packages?

How to create a package.json file?

What Is the Node.js ETL Pipeline?

What is data brokering in Node.js?

How to read and write JSON Files with Node.js?

What is package-lock.json?

How to install Node.js locally with nvm?

How to update Node.js?

How to check unused npm packages?

What is the Node.js fs module?

What is Semantic versioning?

The Basics of Package.json explained

How to patch an NPM dependency

What is NPM audit?

Beginner`s guide to NPM

Getting started with Node.js

Scroll to top ↑