Deleting NPM folders when path is too long

When doing modern web development you will probably have to start using NPM sooner, rather than later. Not a big deal of course, since it’s a great addition to the frontend development environment.

However, most NPM packages have quite a bit of dependencies to other packages. All of these dependencies get pulled towards your system also. Still not a big problem as you want a working solution.

The problem arises when you are on a Windows environment, there are a lot of dependencies and you want to delete a project folder on your system. You will stumble across the fact that Windows has a rather low limit on how long a path can be. When all dependencies are loaded, you will probably have some paths which are too long for Windows to handle properly.

This will give you quite a bit of a problem as you can’t delete the folder(s) anymore. One way of solving this is by using PowerShell. By executing the following script you will be able to delete the NPM modules in your project.

ls node_modules | foreach {
	echo $("Deleting module..." + $_.Name)
	& npm rm $_.Name
}

This will iterate through the node_modules folder and remove each module inside it.

The output will be something like this:

Deleting module...grunt-contrib-uglify
Deleting module...karma

Do note, this only works for NPM folders. If your path is too long because of some other reason, you’ll have to think of something else.


Share