OMBD#15: The Only Command You Need To Know To Wipe Out Your node_modules

Here’s my one-liner to delete all the JavaScript dependencies of a repo in a flash.

Welcome to issue #15 of One Minute Better Developer, where you become a more successful software developer by reading short nuggets of knowledge, one minute at a time.

You want to do a backup of all your current files because you’re switching computers.

Some of your local repositories have unpublished branches so you want to copy them over to your new machine rather than checking out their remote versions.

However, the gazillion files inside node_modules make copying the repo take 2 hours. You don’t want to go on every directory and delete the folder manually.

What do you do?

We are going to use Unix's find to look for node_modules folders in our repositories directory, and then we are going to use xargs to pass these paths to rm.

  1. Go into your repositories directory:
cd <your_repositories_dir_path>

2. List the node_modules directories you are going to remove:

find . -type d -name node_modules# ./example-repo/node_modules
# ./example-repo/node_modules/node-libs-browser/node_modules
# ...

3. Let’s use xargs to pass these paths to rm :

find . -type d -name node_modules | xargs rm -rf

4. Done! let’s double-check that all the node_modules have been deleted:

find . -type d -name node_modules# Empty

Now copying over our repositories directory to our new machine takes just a second, and we can run npm install to generate a new node_modules directory.

Full-Stack Developer, Teacher, Maker, and general Things-Doer. https://www.linkedin.com/in/jonportella/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store