ChatGPT解决这个技术问题 Extra ChatGPT

Delete node_modules folder recursively from a specified path using command line

I have multiple npm projects saved in a local directory. Now I want to take backup of my projects without the node_modules folder, as it is taking a lot of space and can also be retrieved any time using npm install.

So, I need a solution to delete all node_modules folders recursively from a specified path using the command line interface. Any suggestions/ help is highly appreciable.


S
Sebastien H.

Print out a list of directories to be deleted:

find . -name 'node_modules' -type d -prune

Delete directories from the current working directory:

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

Alternatively you can use trash (brew install trash) for staged deletion:

find . -name node_modules -type d -prune -exec trash {} +

-prune is an important optimization. It'll case find not to recurse into node_module directories (to look for nested node_modules)
For a project, I get /node_modules/gulp-server-livereload/node_modules: Directory not empty in a lot of "inner" node_modules folders. How to workaround this?
This is like, I'm serious, the 20th time I am at this answer to copy this code... xD
What is the meaning of '{}' +?
{} is a placeholder which find replaces with the file path it found. + tells find to append all the file paths to a single command rather than running rm for each.
j
jeckep

Try https://github.com/voidcosmos/npkill

npx npkill

it will find all node_modules and let you remove them.

https://i.stack.imgur.com/B6GlX.gif


A better option than above since I usually have 2-3 projects which I want to keep node_modules intact
This should be the top comment!
I prefer to nuke all folders and reinstall them again as needed. Who has time to pick and choose manually?
S
Sidharth

Improving on the accepted answer,

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' +

I found that the command would run a very long time to fetch all folders and then run a delete command, to make the command resumable I'd suggest using \; and to see progress of the command being run use -print to see the directory being deleted.

Note: You must first cd into the root directory and then run the command or instead of find . use find {project_directory}

To delete folders one by one

find . -name 'node_modules' -type d -prune -exec rm -rf '{}' \;

To delete folders one by one and printing the folder being deleted

find . -name 'node_modules' -type d -prune -print -exec rm -rf '{}' \;

Edit:

For the people who like interactive way of doing this refer to @jeckep answer, run this in the directory that you wish to prune.

npx npkill

Nice improvements on the accepted answer @Sidharth, much appreciated
Good job @Sidharth
For (my own) future reference - I wanted to remove the contents of node_modules but leaving the directories in place (to add them to a Dropbox ignore list later). This works for me: find . -name 'node_modules' -type d -prune -print -exec bash -c 'rm -rf "$0"/* "$0"/..?* "$0"/.[!.]*' {} \; The wildcards are for matching hidden files and folders (starting with .) as per unix.stackexchange.com/a/77313/486273
Improving on this answer, using luminS ( github.com/wchang22/LuminS ) – find . -name 'node_modules' -type d -prune -exec lms rm '{}' +
bruh! this does wonders
S
Sumit

I have come across with this solution,

first find the folder using find and specify name of the folder.

execute delete command recursively -exec rm -rf '{}' +

run the following command to delete folders recursively

find /path -type d -name "node_modules" -exec rm -rf '{}' +


n
noseratio

When on Windows, I use the following .BAT file to delete node_modules recursively from the current folder:

@for /d /r . %d in (node_modules) do @if exist %d (echo %d && rd %d /s /q) 

Or, via CMD.EXE:

cmd.exe /c "@for /d /r . %d in (node_modules) do @if exist %d (echo %d && rd %d /s /q)"

I
Ivan V.

bash function to remove node_modules. It will remove all node_modules directories recursively from the current working directory, while printing found paths.

You just need to put in somewhere in your $PATH

rmnodemodules(){

  find . -name 'node_modules' -type d -prune -exec echo '{}' \; -exec rm -rf {} \; 

}

F
Fatih Aziz

if you want to move instead of delete it:

find . -name 'node_modules' -type d -prune -exec mkdir -p ./another/dir/{} \; -exec mv -i {} ./NODE_MODULES/{} \;

this will keep the directory structure.


C
Chau Giang

OS: Ubuntu

A simple trick to remove all node_modules in your servers (which can reduce a lot of space) is to run:

sudo find / -not -path "/usr/lib/*" -name 'node_modules' -type d -prune -exec rm -rf '{}' + 

Here we need to exclude /usr/lib/* because if you wont, it will delete your npm and you need to reinstall it :)


a
aniket dhole

Python Script to Delete the node_modules folder from multiple projects. Just place it in your project folder consisting multiple projects and run it.

import os
import shutil
dirname = '/root/Desktop/proj' #Your Full Path of Projects Folder
dirfiles = os.listdir(dirname)

fullpaths = map(lambda name: os.path.join(dirname, name), dirfiles)

dirs = []

for file in fullpaths:
    if os.path.isdir(file): dirs.append(file)

for i in dirs:
    dirfiles1 = os.listdir(i)
    fullpaths1 = map(lambda name: os.path.join(i, name), dirfiles1)
    dirs1 = []
    for file in fullpaths1:
        if os.path.isdir(file):
            dirs1.append(file)
            if(file[-12:]=='node_modules'):
                shutil.rmtree(file)
                print(file)
    

seems a bit overkill for something that can be done in one line from basic linux programs and probably even windows