If you have worked with Windows development or using Windows as a platform to do development work for other applications, like node.js. You will at some point encounter the issue of “File Name Too Long” error, specifically when your absolute path is exceeding 260 characters, Windows doesn’t let you delete the directory or file.
This is probably one of the oldest legacy issues that are still lingering around in the modern OS, even in Windows 10. One of the biggest problems is when applications or development referencing via a relative path, it doesn’t have a context on how long the absolute path is, it becomes a problem when trying to delete any directory that are longer than 260 characters. This is particularly common when doing node.js development on Windows. Because it’s a cross-platform framework, other OS, like Linux and Mac OS X doesn’t have this restriction. So developer from another platform, might not have this mindset to shorten the directory name so any poor Windows developer would have an easier time to work with the tool. If you’d like to find more info on the history and the reason behind this odd 260 number, you can check out a detailed Windows API documentation that explains all the technical details behind.
Method One: Directory Renaming
So we know the root cause of the problem, a very dumb way to fix it is to rename all the directory to be shorter and fix the limit. However, before you think this is a stupid idea (it is), if all you need is to rebuild node_modules dependencies or any other file path, you only need to change the root directory path. For example, C:\User\yourname\Documents\project\node_modules\…\…\ can become C:\User\yourname\Documents\project\old_node_modules\…\…\
Method Two: Install Node Package rimraf
Use a tool such as npm rimraf, command line version (CLI) of the utility offers Linux/UNIX rm –rf on Windows. It’s a cross-platform npm utility. You need to have node.js installed and run the following command to install this tool via
npm install rimraf –g
Then navigate to the directory where you’d like to remove the node_modules and run
Method Three: Run Cygwin
Similar to method two where we are leveraging Linux/UNIX command to get stuff done, you can actually run native Linux commands through Cygwin. All you need is download and install basic packages with Cygwin.
Once you have Cygwin installed, launch the command prompt and navigate to the path via “/cygdrive/” directory to locate Windows directories.
After successfully located the directory location, you can run
rm –rf node_modules
This will remove all your directory under node_modules silently regardless how long the file name is.