How to clean node_modules folder of packages that are not in package.json?

Cover Image for How to clean node_modules folder of packages that are not in package.json?
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

๐Ÿงน How to Clean Your node_modules Folder

So you've been using npm install to manage your project dependencies, but over time, you realize that you no longer need some of the modules listed in your package.json file. ๐Ÿ“ฆ

Fear not! We've got you covered. In this article, we'll show you how to clean up your node_modules folder and remove any packages that are not in your package.json file. ๐Ÿ—‚๏ธ

The Problem ๐Ÿค”

Let's say you had a project with the following package.json file:

{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1",
    "cors": "^2.8.5",
    "lodash": "^4.17.21",
    "axios": "^0.21.1"
  }
}

But over time, you decided that you no longer needed the lodash package and removed it from your package.json file:

{
  "name": "my-project",
  "version": "1.0.0",
  "dependencies": {
    "express": "^4.17.1",
    "cors": "^2.8.5",
    "axios": "^0.21.1"
  }
}

Here's the catch! The lodash package is still present in your node_modules folder, taking up space and cluttering your project. It's time to clean up and say goodbye to unused packages! ๐Ÿ’ฅ

The Solution ๐Ÿ’ก

Fortunately, there's a handy tool called npm prune that will do the job for you. Just follow these simple steps:

  1. Open your terminal or command prompt.

  2. Navigate to your project directory.

  3. Run the following command:

npm prune

๐ŸŽ‰ Voila! ๐ŸŽ‰ Your node_modules folder has now been cleaned, and only the packages listed in your package.json file remain. No more clutter!

Pro Tip ๐Ÿ’ก

If you want to remove all the installed packages, including the ones listed in your package.json file, you can use the npm ci command instead.

But be cautious with this command! It will remove all the packages and re-install only the ones specified in your package.json file, overwriting any changes you made to your node_modules folder.

Conclusion ๐Ÿ

Cleaning up your node_modules folder doesn't have to be a daunting task. With just a few simple steps and the power of npm prune, you can keep your project tidy and free from unused packages.

So go ahead, give it a try, and enjoy a cleaner and more organized project! Let us know in the comments below if you found this guide helpful or if you have any other cool tips and tricks to share. ๐Ÿ‘‡

Happy coding! ๐Ÿ˜„โœจ


More Stories

Cover Image for How can I echo a newline in a batch file?

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

๐Ÿ”ฅ ๐Ÿ’ป ๐Ÿ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! ๐Ÿš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# The Art of Stripping Punctuation: Simplifying Your Strings ๐Ÿ’ฅโœ‚๏ธ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# Purge or Recreate a Ruby on Rails Database: A Simple Guide ๐Ÿš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? ๐Ÿค” Well, my

Matheus Mello
Matheus Mello