12

The problem:

I've created a yeoman project by mistake on my windows box. Via explorer when I try to delete it I get an error saying that the path is too long.

Source too long error

Several Solutions:

But is there a script based solution?

Community
  • 1
  • 1
deostroll
  • 11,661
  • 21
  • 90
  • 161

6 Answers6

23

You could use rimraf:

npm install -g rimraf
rimraf C:\code\yeoman-foo
Mac
  • 8,191
  • 4
  • 40
  • 51
  • This is insanely helpful!!! I happened to work on someone else's code base recently & I faced the same issue as the OP. I have been reading various articles & trying different things to delete the node_modules folder on Windows but no avail. Added to my woes, there were nested dependencies. I installed the `rimraf` module as you suggested and ran it. Boy, was I pleasantly shocked to see that it deleted the whole node_modules folder in seconds!!! I am only less crying of joy!!! Thanks a ton!!! – Devner Jul 26 '16 at 09:57
10

You should be able to use the force switch. This script recursively removes any node_modules folder using PowerShell 3.

:> ls node_modules -Recurse -Directory | foreach { rm $_ -Recurse -Force }
What Would Be Cool
  • 6,204
  • 5
  • 45
  • 42
  • This is an ideal solution, this will delete node_modules from sub-folders also. i'm just after running it on my dev directory. – Adil H. Raza Mar 05 '19 at 18:01
3

You can write powershell to this effect relying on npm

PS C:\code\yeoman-foo> ls node_modules | foreach {
>> echo $("Deleting module..." + $_.Name)
>> & npm rm $_.Name
>> }
>>

After the above command completes you can remove the folder by the traditional ways...

Go to the parent folder containing the project folder, select it, and SHIFT + DEL

Kieren Johnstone
  • 41,277
  • 16
  • 94
  • 144
deostroll
  • 11,661
  • 21
  • 90
  • 161
1
  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

j_quelly
  • 1,399
  • 4
  • 16
  • 37
  • Thanks! This does take some time, but IS quicker than deleting from file explorer and doesn't move folder to recycle bin. – BanAnanas Sep 14 '20 at 13:44
1

Easiest way I found so far (no installing or separate programs required) is to just run these commands in the root of your project (next to the node_modules folder):

mkdir temp_dir
robocopy temp_dir node_modules /s /mir
rmdir temp_dir
rmdir node_modules

For convinience you can also put this code in a .bat file and place it in the project root and run it whenever you want to remove the entire node_modules map

Arno van Oordt
  • 2,912
  • 5
  • 33
  • 63
-2

Try this rmdir node_modules /s /q

Stuart Hallows
  • 8,795
  • 5
  • 45
  • 57