DEV Community

Sifat Faysal
Sifat Faysal

Posted on • Updated on

Check & remove unused npm packages

Cleaning Up Unused npm Packages in Your Project

As a developer, it's easy to accumulate a large number of npm packages in your project over time, many of which may no longer be needed. These unused packages not only add clutter to your project, but they can also slow down your build times and increase the size of your application. In this post, we will explore a few ways to find and remove unused npm packages in your project.

The first step in cleaning up your npm packages is to find out which packages are not being used in your code. One way to do this is to use the npm package "depcheck". To install depcheck, run the following command in your project's root directory:

npm install --save-dev depcheck
Enter fullscreen mode Exit fullscreen mode

Once depcheck is installed, you can run it with the following command:

npx depcheck
Enter fullscreen mode Exit fullscreen mode

This will generate a report of all the unused packages in your project, as well as any missing dependencies. You can also run it with additional option to show the detailed report

npx depcheck --detailed
Enter fullscreen mode Exit fullscreen mode

Another tool that can help you find unused packages is "npm-check-updates". This tool can help you check if there are any packages in your project that can be updated. To install npm-check-updates, run the following command:

npm install -g npm-check-updates
Enter fullscreen mode Exit fullscreen mode

Once npm-check-updates is installed, you can run it with the following command:

ncu
Enter fullscreen mode Exit fullscreen mode

This will generate a list of all the packages in your project that can be updated, as well as the version numbers of the latest available updates. You can also use the -u option to upgrade the packages to the latest version.

ncu -u
Enter fullscreen mode Exit fullscreen mode

Now that you have a list of unused packages, you can remove them from your project. You can do this manually by deleting them from your package.json file and running "npm prune" command. This command removes the packages that are no longer listed in package.json file and are not in use.

npm prune
Enter fullscreen mode Exit fullscreen mode

Another way to remove the packages is by using "npx" command:

npx npm-check-updates -u -a --prune
Enter fullscreen mode Exit fullscreen mode

This command will automatically update all the packages and remove the unnecessary packages.

It's always a good practice to keep your project's dependencies up to date and remove the unused packages. With the tools and methods outlined in this post, you can easily find and remove unused npm packages in your project, keeping it clean and optimized for better performance.

Note: Before removing any packages, make sure that you test the project again to ensure that everything is working as expected.
Also, remember to commit the changes after removing the unnecessary packages, so the other team members also use the updated version

Top comments (1)

Collapse
 
ahmadmaartmesrini profile image
Ahmad Maartmesrini • Edited

Thank you so much Sifat!