Search code examples
node.jswindowsvisual-studionpmgulp

Windows Error Deleting node_modules Folder: Source Path Too Long


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:

enter image description here

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?


Solution

  • 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.