3

I have a pretty complicated project that has dependencies and sub dependencies. I have git submodules within my project that have npm dependencies within them.

Is there a quick and effective way to clean all of the node_modules folders in my project recursively?

nikjohn
  • 20,026
  • 14
  • 50
  • 86

3 Answers3

1

Just clear all dependencies you want to remove in your package.json file and then run npm prune.

Bilal
  • 2,645
  • 3
  • 28
  • 40
  • I have `git` submodules within my project that have npm dependencies within them. On the highest level, what you said would work, or I could just delete my `node_modules` folder. But this wouldn't affect my submodules – nikjohn Aug 18 '16 at 10:53
  • 1
    I think this would help to remove all dependencies. http://stackoverflow.com/questions/31773546/the-best-way-to-run-npm-install-for-nested-folders – Bilal Aug 18 '16 at 11:36
  • Ah thanks. That's slightly convoluted, but does the job!! – nikjohn Aug 19 '16 at 05:13
1

If you're asking how to get rid of the nested mess of node_modules there are a couple ways to do this:

  1. install rimraf globally, then call rimraf [directory-path]
  2. Select the folder in WIndows Explorer, hold Shift + Delete, it will recursively delete the folders
  3. Upgrading to newer versions of Node and NPM collapse the folder structure so it's more linear, which is pretty awesome (this would be my recommended solution and how I solved it at my company)
Brandon Parker
  • 762
  • 7
  • 18
0

if you are using unix system, basically you can run this command find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

Or use this script https://gist.github.com/qutek/868c30f5d1e7ec03c7459e067444bd67

Just download it and make it executable chmod +x clear-node-modules.sh and run it ./clear-node-modules.sh

Lafif Astahdziq
  • 3,788
  • 29
  • 38