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
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
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.
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.