Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

488
Views
How to find all node_modules directories in a given directory and it's sub folders, then delete? (Node.js/fs)

I just recently was introduced to fs as I'm somewhat of a novice. My goal is to write a program to find all node_modules folders within a given directory and it's subfolders. For example, I have a directory that holds all of my project directories and in each of those I have node_modules directories I would like to clean up regularly by writing my own tiny program using fs & node, so I can navigate to the directory (or any directory) and run my command to delete these for me on the fly instead of trying to use file explorer to delete all which usually just locks up Windows Explorer altogether.

I've figured out the simplest form of this which is just going into that directory specifically and deleting. But I need to search all subfolders, find the node_modules directories, and then delete them all.

Here is what I've implemented that works, but only for the given directory I am currently in.

import fs from 'fs-extra';

const folder = './node_modules';

fs.remove(folder, err => {

    if (err) {
        console.log("You messed up ya dummy, so read this ---> ", err);
    } else {
        console.log("You deleted those gosh darn node thingys. Congrats, you're a genuis.");
    }

});

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!