removing node_modules folder
Asked Answered
E

6

13

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?

Exodus answered 28/4, 2015 at 9:19 Comment(0)
E
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

Exodus answered 28/4, 2015 at 9:19 Comment(0)
M
24

You could use rimraf:

npm install -g rimraf
rimraf C:\code\yeoman-foo
Motor answered 4/5, 2015 at 5:46 Comment(1)
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!!!Samellasameness
U
11

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 }
Uruguay answered 13/10, 2016 at 1:31 Comment(1)
This is an ideal solution, this will delete node_modules from sub-folders also. i'm just after running it on my dev directory.Dressingdown
E
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

Exodus answered 28/4, 2015 at 9:19 Comment(0)
R
2
  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

Rafaelof answered 18/2, 2016 at 16:49 Comment(1)
Thanks! This does take some time, but IS quicker than deleting from file explorer and doesn't move folder to recycle bin.Agonist
R
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

Reside answered 25/2, 2017 at 12:51 Comment(0)
H
-2

Try this rmdir node_modules /s /q

rmdir is a native command in powershell. Used to Remove-Item. /s flag indicates subfolders /q flag indicates quite mode

Hydrostatics answered 4/3, 2020 at 3:12 Comment(2)
What does /s and /q do ? consider explaining ur answer so that others including me can read understandUntidy
@ShamseerAhammed /s means to include folders and subfolders inside the folder. /q means quite mode.Dimphia

© 2022 - 2024 — McMap. All rights reserved.