Assume I install project packages with npm install
that looks into package.json
for modules to be installed. After a while I see that I don\'t need
I have added few lines inside package.json:
"scripts": {
...
"clean": "rmdir /s /q node_modules",
"reinstall": "npm run clean && npm install",
"rebuild": "npm run clean && npm install && rmdir /s /q dist && npm run build --prod",
...
}
If you want to clean
only you can use this rimraf node_modules
or rm -rf node_modules
.
It works fine
You could remove your node_modules/ folder and then reinstall the dependencies from package.json.
rm -rf node_modules/
npm install
This would erase all installed packages in the current folder and only install the dependencies from package.json. If the dependencies have been previously installed npm will try to use the cached version, avoiding downloading the dependency a second time.
Due to its folder nesting Windows can’t delete the folder as its name is too long. To solve this, install RimRaf:
npm install rimraf -g
rimraf node_modules
The best article I found about it is this one: https://trilon.io/blog/how-to-delete-all-nodemodules-recursively
All from the console and easy to execute from any folder point.
But as a summary of the article, this command to find the size for each node_module
folder found in different projects.
find . -name "node_modules" -type d -prune -print | xargs du -chs
And to actually remove them:
find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;
The article contains also instructions for windows shell.
Remove/Edit the packages that are not needed in package-lock.json (package names will be written under dependencies & devDependencies) and then
npm install
Have you tried npm prune?
it should uninstall everything not listed in your package file
https://npmjs.org/doc/cli/npm-prune.html