Windows Error Deleting node_modules Folder: Source

2019-04-29 07:42发布

问题:

I've made the switch to use npm/gulp in Visual Studio on Windows. However, removing the associated files has been painful in that I cannot easily delete the node_modules folder.

When trying to delete a folder hierarchy for a solution using Windows Explorer, I get the following "Source Path Too Long" dialog:

From there, I've tried using the Windows command prompt to delete using

rmdir /s /q node_modules

which may or may not work. When it doesn't work, the errors look something like

(snip)
~1\NODE_M~1\read-pkg\NODE_M~1\PATH-T~1\readme.md - The file name is too long.

node_modules\GULP-I~2\NODE_M~1\imagemin\NODE_M~1\IMAGEM~1\NODE_M~1\OPTIPN~1\NODE_M~1\logalot\NODE_M~1\squeak\NODE_M~1\LPAD-A~1\NODE_M~1\meow\NODE_M~1\redent
\NODE_M~1\INDENT~1\NODE_M~1\REPEAT~1\NODE_M~1 - The directory is not empty.

node_modules\GULP-I~2\NODE_M~1\imagemin\NODE_M~1\IMAGEM~1\NODE_M~1\OPTIPN~1\NODE_M~1\logalot\NODE_M~1\squeak\NODE_M~1\LPAD-A~1\NODE_M~1\meow\NODE_M~1\redent
\NODE_M~1\INDENT~1\NODE_M~1\REPEAT~1\package.json - The file name is too long.
(snip)

The only consistently successful way that I've found to remove the folder hierarchy is to go into subfolder after subfolder, renaming each folder to something short like 'a'. Eventually the path is short enough to allow deletion. This can waste quite a bit of time.

I've seen references to preventing the problem, but my question is about easily deleting the folder hierarchy that contains the problematic path length.

In short, the question is:

Is there a simple way to delete a folder hierarchy in Windows that gets the "Source Path Too Long" error?

回答1:

This is the simplest option I've found so far:

npm install -g rimraf

then

rimraf node_modules

From there the folder hierarchy should be able to be deleted.

This option requires installing the rimraf package. A solution without needing the package would be nice, but I haven't found a simple one.



回答2:

Robocopy can do this as well... Here is the Regkey I put in to be able to right click-> delete any folder using robocopy which will delete npm_module folders

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\RoboDelete]

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shell\RoboDelete\command]
"Extended"=""
@="\"C:\\windows\\delfolder.bat\" \"%1\""

and here is the Bat file content

@echo off
if {%1}=={} @echo Syntax: DelFolder FolderPath&goto :EOF
if not exist %1 @echo Syntax: DelFolder FolderPath – %1 NOT found.&goto :EOF
setlocal
set folder=%1
set MT="%TEMP%\DelFolder_%RANDOM%"
MD %MT%
RoboCopy %MT% %folder% /MIR
RD /S /Q %MT%
RD /S /Q %folder%
endlocal

Hope this helps ..