21

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

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
  • 1
    It should remove it from node_modules. – Andrew Li Jul 10 '18 at 22:25
  • 1
    It didn't remove on my machine & I searched for a while until I found this solution to remove unused dependencies without manually deleting the folder – Ahmed Elkoussy Jul 10 '18 at 22:29
  • ok, for a beginner in npm, it is not clear that npm uninstall won't remove from node_modules folder, even in documentation of npm prune it is using description that isn't straightforward, it took me around half an hour to know why npm uninstall didn't remove it and even from the other commenter here it seems he thought the same (that npm uninstall will remove the package folder) – Ahmed Elkoussy Jul 11 '18 at 00:14

3 Answers3

24

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

E_net4
  • 27,810
  • 13
  • 101
  • 139
Ahmed Elkoussy
  • 8,162
  • 10
  • 60
  • 85
  • That's very useful, I wonder why there are downvotes with no clarification :) – Mustafah Jul 11 '18 at 07:28
  • 2
    npm prune not working for me, npm uninstall (package_name) -s this is working – skyshine Jul 12 '19 at 06:49
  • npm prune does not work for global packages because there is not a package.json file. I can't find a solution, npm uninstall -g does not remove the package folder in node_modules. Then, npm list -g --depth 0 shows some errors because finds the folder but not the package.json file (the folder remains, but it's empty). – blow Nov 17 '19 at 09:55
  • My pleasure, glad I could help – Ahmed Elkoussy Mar 01 '20 at 10:10
1

If you tried both npm prune and npm uninstall --save but the modules still exist in node_modules, try to remove the node_modules folder:

rm -rf node_modules

and then run

npm install

to rebuild your node_modules again with only the modules you need

fatiha.kafou
  • 71
  • 1
  • 13
0

I like to share my struggle in uninstalling a package from my project. I believe it may add value to the answers above.

I evaluated a package named react-router in one of my projects. Later I decided to remove it. I wanted to remove the package first and then remove its reference from my code. I tried npm uninstall and npm prune. However, in both cases, it failed to remove the package from my node-modules folder. I removed the reference of react-router from the code and tried again. I still couldn't remove it with the above command. After some digging, I found that I also installed react-router-dom that uses the package react-router. So I had to remove it first using the commands above. Only then, I was able to remove the react-router.

I find it strange that npm uninstall does not remove packages if they are referred. I think, if it allows, I don't have to manually find the usage of the package by searching them in class instead of IDE/Lint can help me find it much more easily.

Ripal Barot
  • 687
  • 8
  • 16