I have tried to remove a package by using
npm uninstall (package_name) -s
Which removed the package from package.json but didn't remove it from node_modules folder, so how can I remove these unused package from node_modules folder?
Edit:
I am sharing this Q&A as I faced this issue personally, having used all variations of npm uninstall
but it didn't remove the package from node_modules, so I shared what helped me remove about 10 unused packages which was npm prune
This uninstalls a package, completely removing everything npm installed on its behalf. It also removes the package from the dependencies, devDependencies, optionalDependencies, and peerDependencies objects in your package. json. Further, if you have an npm-shrinkwrap.
To remove a package with the npm uninstall command, you can use the syntax npm uninstall package-name in the directory where the package is located.
First, you must delete the dependency from your node_modules/ folder, and second, remove its listing from your package. json. This ensures the package is fully removed. Instead of performing this task manually, we can use the npm uninstall command.
There are two ways to clean up the node_modules folder: Delete the folder and reinstall. Use npm prune (starting with npm version 6)
As per the npm guide, npm uninstall removes the package from dependencies only, it will not remove the package folder from node_modules (that is not mentioned in the description anyway)
For some reason I thought the npm uninstall will remove the package folder from node_modules too, but it didn't happen, after some research I found that we should use
npm prune
This command will remove unused packages from node_modules automatically as per the official npm description
that the npm uninstall will only remove the package from your package.json file but it will not delete the package from your node_modules
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With