How to Delete Deep Nested Folder like node_modules

2019-01-15 23:48发布

问题:

Upon trying to remove the node_modules directory created by npm install:

The source file name(s) are larger than is supported by the file system. Try moving to a location which has a shorter path name, or try renaming to shorter name(s) before attempting this operation

I also tried shift + delete and still having the same issue.

回答1:

Since this the top google result, this is what worked for me:

Install RimRaf:

npm install rimraf -g

And in the project folder delete the node_modules folder with:

rimraf node_modules

[ http://www.nikola-breznjak.com/blog/nodejs/how-to-delete-node_modules-folder-on-windows-machine/ ]



回答2:

I've simply done that by using Winrar, this may seem a strange solution but working very well.

  • right click on node_modules folder
  • select Add to archive ... from the menu.
  • Winrar dialog opens
  • just check the option delete files after archiving
  • Don't forget to delete the node_modules.rar after finished.


[UPDATE] This also works with 7Zip




回答3:

Create an empty folder "test" on C or D drive and use following DOS command

robocopy /MIR c:\test D:\UserData\FolderToDelete > NUL

After completing above command, your folder will be empty, now you can delete the folder.

Don't worry your test folder will always be empty, so you can delete it at any time.



回答4:

You can use Git Bash to remove the folder:

example: c:\users\olefrank\projects\mynodeproject

rm -rf /c/users/olefrank/projects/mynodeproject



回答5:

Delete Deep Netsted Folder like node_modules in Windows

  1. Option 1: Detele without installing anything

    • Create a folder with name test in any Drive

      robocopy /MIR c:\test D:\UserData\FolderToDelete > NUL

    • delete the folder test and FolderToDelete as both are empty

  2. Option 2 Delete using rimraf NPM package

    • Open command prompt and change your directory to the folder where node_modules folder exists.

    • Run

      rimraf node_modules

    • Missing rimraf ERROR then Install

      npm install rimraf -g

    • When the installation completes, run

      rimraf node_modules

NOTE: One of the deep nested folder structure is node_modules, Windows can’t delete the folder as its name is too long. To solve this, Easy solution, install a node module RimRaf



回答6:

I used GitBash to remove de folder!

rm -r node_modules

It took a while to delete everything, but worked for me!



回答7:

I had a similar problem and RD didn't work, for some unknown reason.

NPM can get rid of its own mess though, so if you do npm uninstall [module-name] for each directory in node_modules, you'll get rid of them.

(I'll look up how to batch loop this later, for those who have lots of dependencies.)



回答8:

You can use Git Bash to remove the folder:

example: c:\users\stu\projects\mynodeproject

rm /c/users/stu/projects/mynodeproject -rfd



回答9:

Try Visual Studio Code

After trying many solution i find this one is pretty simple. just open the project in Visual code and delete it. the UI may freeze for some seconds but it will definitely works.I test using many large size node_modules folder with it

Thanks



回答10:

  1. npm install -g remove-node-modules
  2. cd to root and remove-node-modules
  3. or remove-node-modules path/to/folder

Source:

https://github.com/j-quelly/node-cleanup



回答11:

On Windows, using Total Commander all you have to do is select the folder click shift + delete . Don't forget about the shift key.



回答12:

simple just run for windows I haven't tested it for other operating systems

rm -r node_modules

in fact, you can delete any folder with this.

like rm -r AnyFolderWhichIsNotDeletableFromShiftDeleteOrDelete.

just open the gitbash move to root of the folder and run this command

Hope this will help.

Thanks, Ajay Kotnala



回答13:

I just do del node_modules inside my project folder on PowerShell, it will ask you if you want to remove it and its children folder, just hit 'Y' and that's it



回答14:

Any file manager allow to avoid such issues, e.g Far Manager



回答15:

One solution that I use:

(I would prefer to avoid installing a new extension (rimraf) when working with CI environments.)

  1. A) Rename packages.json to something else. B) Specially on CI - after npm install, I usually remove the file instead of renaming it, but if you need it, you don't have to do this. That's your choice.
  2. run npm init - this will create an empty packages.json file (no dependencies)
  3. run npm prune - this will match node_modules with the dependencies section of packages.json - which is now empty as the result of step #2.
  4. If you have chosen #1.A. step, delete the newly created packages.json, and rename original packages.json back to its original name.


回答16:

Its too easy.

Just delete all folders inside node_modules and then delete actual node_module folder.

This Works for me. Best luck....



回答17:

On Windows my go to solution is using the rmdir command:

rd /S .\node_modules\

If it fails the first time -- try one more time. Also check if you have running scripts currently using the modules (npm run serve or similar).



回答18:

From this looks of this MSDN article, it looks like you can now bypass the MAX_PATH restriction in Windows 10 v1607 (AKA 'anniversary update') by changing a value in the registry - or via Group Policy



回答19:

Just use powershell..

Run powershell and cd to the parent folder and then:

rm [yourfolder]

as in:

rm node_modules


回答20:

I'm on windows 10 and I could'nt delete folders with message "directory not emtpy". Neither rimraf nor rm -rf worked.

Copying an empty text file to every single folder did the trick - I was able to delete the complete node_modules folder.



回答21:

Sometimes, even if you install rimraf globally you can have a local rimraf dependency (SASS usually have it). In this case I would run following commands:

Follow first 2 steps as usagidon recommended, if you have issues or errors try

npm uninstall rimraf & rimraf node_modules

this will delete local rimraf and use the global one



回答22:

Not exactly related, but as this is the first post I found in my search for a similar solution I think it's worth posting here.

I was running into permission issues on Windows 10 trying to delete my node_modules folder after a failed attempt at installing electron-redux. It seems electron-redux added @types to my node_modules, which had incorrect permissions set up.

rimraf did not work as it still ran into permission issues.

I ended up renaming node_modules then a simple delete worked.



回答23:

Okay so my problem was that i had .bin folder inside node_modules and i wasn't able to delete that, I tried all the above solutions for the same but nothing worked.

Error which was repeating was "u do not have permission to delete the folder or its been used by some other program".

Solution that i found "I had Antivirus installed on my pc ,which was unknowingly using the files from node_modules folder".

I uninstalled the antivirus or u can change permission setting from antivirus setting option (depends on which antivirus u have).Now i am able to delete node_modules folder.



回答24:

Advance a little beat for multiple folders...

If you have a project folder with multiple 'builds' that contains 'node_module'. Like:

C:/Project/build-1/node_modules
C:/Project/build-2/node_modules
C:/Project/build-3/node_modules
C:/Project/build-4/node_modules

You want to delete all folders starts with "build"

! First install nodejs and rimraf as suggested above !

Then go to path with project and run PowerShell command

cd  C:/Project/
Get-ChildItem build* | foreach {rimraf -Path $_.FullName}

It will take a wile



回答25:

go to your path using cmd where your node_modules folder and apply below command

npm install rimraf -g 
rimraf node_modules