Michael Whelan

behaviour driven blog

Deleting Nested node_modules Folders

When using Node Package Manager (NPM) on Windows, it has an annoying habit of creating massively nested node_modules folders that exceed the 260 character path length limit. This makes it extremely painful to try to delete and causes problems with most windows tools. In Visual Studio, for example, I cannot Refresh Folder when I am in a website project (as opposed to a web application project).

Fortunately, the fix is pretty simple.

First, you need to install RoboCopy. If you are on Windows 8, I suggest you use the version from the Windows Server 2003 resource kit as there appear to be issues with the Windows 8 version. Next, create an empty directory and use the RoboCopy mirror flag to mirror the empty directory to the problematic node_modules folder. You can then delete the empty directory in the normal way. Cunningly, deleting the mirrored directory also deletes the node_modules directory.

robocopy new_empty_directory node_modules /MIR

Credit to toby1kenobi for this clever solution!

Finally, you just have to restore the deleted node packages. From the application directory, just run npm update:

npm update

This command will update all the packages listed in the packages.json file to the latest version. It will also install missing packages.

About Michael Whelan

Michael Whelan is a Technical Lead with over 20 years’ experience in building (and testing!) applications on the Microsoft stack. He is passionate about applying agile development practices, such as BDD and continuous delivery, to agile processes. These days his primary focus is ASP.Net MVC Core and Azure. He contributes to a number of open source frameworks through TestStack.

comments powered by Disqus