• Skip to main content
  • Skip to primary sidebar

Ryan McCormick

Dedicated Dad, Software Engineer and Lover of Coffee

Clear all Node Modules Folders Recursively Mac/Linux

January 10, 2018 by Ryan McCormick 4 Comments

If you do any kind of front end development or front end development training, you have probably accumulated a bunch of projects with aging/stale “node_modules” directories. Are you going to take time to open every single old project you have to nuke the node_modules directory? Nobody has time for that.

I wanted to migrate a bunch of old code to an archive drive and because of the size of each “node_modules” directory, I didn’t care to save “node_modules” archives with my old projects (especially since I can just run npm install to reinstall if needed).

Clear all node_modules Directories | Mac and Linux

Since I don’t have any control over how individual users use, input, modify or copy/paste the node_modules nuke command, it is only reasonable that I mention that I take zero responsibility for the outcomes of the use, misuse, etc… you get the picture.

TL;DR: I saved about 5GB after running this command:

  1. Open your terminal.
  2. Browse to your master project directory or the top level of where you have a bunch of projects stored.
  3. Run the command: find . -name "node_modules" -type d -prune -exec rm -rf '{}' +

How it works

Since this blog is driven towards educating about the process of solving problems versus just providing blind answers, lets take some time to break this command down into pieces to examine how it works.

  1. find . Instructs the find program to search for files in the current directory ..
  2. -name "node_modules" Instructs the find program to search for files named “node_modules”.
  3. -type d Instructs the find program to only look for file directories named “node_modules”.
  4. -prune Instructs the find program to not descend into the current file/directory for it to exclude child “node_modules” directories. On Mac, since -d was specified, -prune has no effect on the find program’s lookup results (per the man pages). However, on Linux, -prune still instructs the find program to not descend into the current file/directory.
  5. -exec rm -rf '{}' + Instructs the find program to execute rm -rf on the matching results. The code '{}' + instructs the command line to be built by appending each selected file name at the end thus invoking the rm -rf command less times than the total number of “node_modules” directory matches. This helps with performance.

And there you have it. As I mentioned above, I saved ~5GB by running this command. If you have a lot of projects, you could easily clear up more than ~5GB.

Related

Filed Under: Linux, MacOS Tagged With: linux, MacOS, node_modules, osx

Reader Interactions

Comments

  1. ahmedmhashem says

    April 20, 2019 at 9:04 am

    Thanks, cool one 😉

    Reply
  2. Tim Ross says

    June 18, 2019 at 3:13 am

    Thanks, that’s a pretty useful trick. Saved me a ton of space from my old projects.

    Reply
  3. Qaisar Irfan says

    August 6, 2020 at 9:00 am

    Saved 10+ GB 🙂

    Reply
  4. jusopi says

    August 6, 2020 at 9:07 pm

    Is there anyway to have it print out the results of what it finds? Basically to provide some sort of visual feedback that it is indeed working? I have about 16GB of node_modules getting nixed and it’s just sitting there at the moment.

    Reply

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Force Quit Kill all Chrome Windows MacOS
  • SOLVED: Angular 6 CLI Karma Stuck in Single Run | Karma Stops Running
  • How to Manually Install Java 8 on Ubuntu 18.04 LTS
  • Remove VirtualBox from Ubuntu 16.04 Xenial
  • Clear all Node Modules Folders Recursively Mac/Linux

Recent Comments

  • KKV on Webstorm adding spaces between imports and braces | JavaScript and TypeScript
  • jusopi on Clear all Node Modules Folders Recursively Mac/Linux
  • Qaisar Irfan on Clear all Node Modules Folders Recursively Mac/Linux
  • mustafa on Remove VirtualBox from Ubuntu 16.04 Xenial
  • Pourya on How to Manually Install Java 8 on Ubuntu 18.04 LTS

Archives

  • May 2019
  • May 2018
  • April 2018
  • March 2018
  • January 2018
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • March 2017
  • December 2015
  • November 2015
  • July 2015
  • April 2015
  • February 2015
  • September 2014
  • June 2014
  • May 2014
  • April 2014
  • March 2014
  • February 2014
  • October 2013
  • August 2013
  • June 2013
  • April 2013
  • March 2013
  • February 2013
  • December 2012
  • October 2012
  • September 2012
  • August 2012
  • July 2012
  • May 2012
  • March 2012
  • February 2012
  • December 2011
  • November 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • August 2009
  • July 2009
  • May 2009

Categories

  • Angular
  • Angular 2
  • AngularJS (1x branch)
  • Computer Q&A
  • ES2015
  • Internet Marketing
  • Javascript
  • Job Interviews
  • Job Search
  • Karma
  • Laravel
  • Linux
  • Linux/Unix Tips
  • MacOS
  • Microsoft Access
  • Microsoft Excel
  • Microsoft Outlook
  • Microsoft Word
  • News
  • Node
  • Open Source
  • PHP
  • Protractor
  • Resume Writing
  • Spring Boot
  • SQL
  • Ubuntu
  • VBA
  • VBScript
  • VirtualBox
  • Web Development
  • Windows Tips
  • Wordpress

Meta

  • Log in
  • Entries feed
  • Comments feed
  • WordPress.org

Copyright © 2023 · Magazine Pro on Genesis Framework · WordPress · Log in