How to clean node_modules folder of packages that are not in package.json?
๐งน 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:
Open your terminal or command prompt.
Navigate to your project directory.
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! ๐โจ