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

4

24

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

Chairmanship answered 10/7, 2018 at 22:21 Comment(3)
It should remove it from node_modules.Ritualize
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 folderChairmanship
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)Chairmanship
C
26

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

Chairmanship answered 10/7, 2018 at 22:21 Comment(6)
That's very useful, I wonder why there are downvotes with no clarification :)Onaonager
npm prune not working for me, npm uninstall (package_name) -s this is workingIntromit
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).Lhasa
My pleasure, glad I could helpChairmanship
It is also possible, while removed from your own package.json, the library you are trying to remove is used as a submodule in another package. In this case it will remain in node_modules, even though you are not using it directly. To check usages: npm list <myPackageName[@2.0.0]>Salaried
that's great but wasn't it supposed to remove it from package-lock.json too then, dammit?Habana
G
2

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

Grilled answered 11/10, 2021 at 9:36 Comment(1)
This didn't work for me. I used yarn temporarily, yarn uninstall will remove the dependency from both package.json file and node_modules folderAuroraauroral
U
1

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.

Unprejudiced answered 14/6, 2021 at 9:36 Comment(0)
P
0

I had a very strange experience with npm uninstall. I have two packages A and B where A depends on B.

npm uninstall B does not work (as observed by Ripal Barot above), if A is present.

npm uninstall A works. Next when I go to uninstall B, it reinstalls A again (I am using --no-save option in all cases as I don't want my package.json to be modified and I don't have any package-lock.json).

The only solution I could find was:

  1. Physically delete the directory containing both the above packages
  2. Remove the packages from npm cache (npm cache clear , which I got by npm cache ls @version.
  3. Reinstall the packages with npm install

I really could not find any other way of removing the traces of the package

Plainsman answered 27/2, 2024 at 10:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.