Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

npm uninstall removes the package from package.json but doesn't remove it from node_modules folder

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

like image 978
Ahmed Elkoussy Avatar asked Jul 10 '18 22:07

Ahmed Elkoussy


People also ask

Does npm uninstall remove from package json?

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.

How do I force an npm package to uninstall?

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.

How do I remove a package from node modules?

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.

Can I delete node_modules folder?

There are two ways to clean up the node_modules folder: Delete the folder and reinstall. Use npm prune (starting with npm version 6)


1 Answers

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

like image 162
Ahmed Elkoussy Avatar answered Oct 29 '22 07:10

Ahmed Elkoussy