5

So, I should delete all node_modules in my project(I want that the node_modules folder become completely empty). Just removing folder manually does not suit me.

I read that I can delete it with rm -rf node_modules/ BUT it does not work on WINDOWS.

How to delete it?

3

5 Answers 5

10

Deleting node_modules is as simple as writing the node_modules without a slash:

rm -rf node_modules

rm -rf node_modules shouldn't have a slash at the end /, and this worked for me even on Widows.

Sign up to request clarification or add additional context in comments.

3 Comments

This simple command works in Git Bash also rm -rf node_modules.
The -rf option tells it to also recursively delete non-empty directories and subdirectories, forcefully
That should be peculiar to you, because, all the time, I had the course to use -rf prefix, it has never deleted any other file, say the specific file or folder it is prefixed with. Forcefully, yeah, but using just rm gives you a different error message.
6

Installing globally rimraf will do the job.

npm install -g rimraf

From the docs:

If installed with npm install rimraf -g it can be used as a global command rimraf [ ...] which is useful for cross platform support.

So with installing this package, you can remove directories on all platforms(windows, linux).

All left to do is rimraf node_modules

Comments

1

I have seen a number of developers facing issues with disk space being consumed heavily by 'node_modules' folders, especially when working with JavaScript projects. To assist with this, I have developed a Python script that quickly and efficiently deletes all 'node_modules' folders in a directory and its immediate subdirectories.

The script uses multi-threading to enhance the speed of the deletion process.

import os
import shutil
from concurrent.futures import ThreadPoolExecutor

def delete_node_modules_path(node_modules_path):
    try:
        shutil.rmtree(node_modules_path)
        print(f"Successfully deleted the folder: {node_modules_path}")
    except Exception as e:
        print(f"Could not delete the folder {node_modules_path} due to the following error: {e}")

def delete_node_modules(base_path):
    # Verify if the provided path exists
    if not os.path.exists(base_path):
        print(f"The specified path does not exist: {base_path}")
        return

    # Get a list of all immediate subdirectories
    subdirs = [os.path.join(base_path, d) for d in os.listdir(base_path) if os.path.isdir(os.path.join(base_path, d))]
    
    # Traverse only one level down the directory structure and delete 'node_modules' folders
    with ThreadPoolExecutor() as executor:
        for subdir in subdirs:
            node_modules_path = os.path.join(subdir, 'node_modules')
            if os.path.exists(node_modules_path):
                print(f"Processing: {subdir}")
                executor.submit(delete_node_modules_path, node_modules_path)

if __name__ == "__main__":
    base_path = os.getcwd()
    print(f"The current base path is: {base_path}")
    delete_node_modules(base_path)

Comments

0

try this for Windows

Get-ChildItem -Path . -Filter "node_modules" -Directory -Recurse | Remove-Item -Force -Recurse

Comments

0

we must separate the -r from the -force. We all write the word force, because the -f is ambiguous. Indeed, the -f can mean either -Filter or -Force

rm -r -force node_modules

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.