Skip to content

node_modules on Windows is worse than on a Mac, and the reason is paths

The same folders, plus a path length limit that breaks tools. How to find them all and the setting that stops the errors.

5 min read

Every Node project keeps its dependencies in a folder beside the source, and a modest project is a few hundred megabytes. The Windows specific part is that deep dependency trees run into a path limit that does not exist on other platforms.

Finding all of them

Get-ChildItem $env:USERPROFILE -Recurse -Directory -Filter node_modules -Force -EA SilentlyContinue |
  Where-Object { $_.FullName -notmatch 'node_modules.*node_modules' } |
  ForEach-Object {
    $s = (Get-ChildItem $_.FullName -Recurse -Force -File -EA SilentlyContinue | Measure-Object Length -Sum).Sum
    [PSCustomObject]@{ MB = [math]::Round($s/1MB); Path = $_.FullName }
  } | Sort-Object MB -Descending | Select-Object -First 20

The filter excludes nested folders so each project is counted once by its outermost one, which is the number that matters.

Deleting them is slow on Windows, and there is a reason

A node_modules folder is tens of thousands of tiny files, and Explorer deletes them one at a time with a progress dialog that can take minutes. The command line is dramatically faster:

# In the project folder
cmd /c "rmdir /s /q node_modules"

For many projects at once, the same find command with a removal step. Everything is restored by an install, so the cost is bandwidth and time.

The path limit that breaks installs

Windows historically limited paths to 260 characters, and a deep dependency tree exceeds that easily. The symptom is an install or a delete failing with a path too long error on a file nobody has ever heard of.

  1. Enable long path support: in Group Policy, Computer Configuration, Administrative Templates, System, Filesystem, Enable Win32 long paths.
  2. Or set the registry value LongPathsEnabled to 1 under HKLM\SYSTEM\CurrentControlSet\Control\FileSystem.
  3. Restart.
  4. Use a package manager that flattens the tree, which modern npm, yarn and pnpm all do.
Set-ItemProperty 'HKLM:\SYSTEM\CurrentControlSet\Control\FileSystem' -Name LongPathsEnabled -Value 1

Keeping projects shallow

Checking repositories out near the root, such as C:\src, rather than deep under Documents, avoids most path problems before they start. It looks like superstition and it is the single most effective habit on a Windows development machine.

The shared cache behind all of these is the npm cache, and the same folders on the other platform are covered in node_modules across every project.

Common questions

How do I delete node_modules on Windows?

Use rmdir /s /q node_modules from a command prompt rather than Explorer, which deletes tens of thousands of small files one at a time and takes far longer.

Why do I get path too long errors on Windows?

Because the classic path limit is 260 characters and deep dependency trees exceed it. Enable Win32 long paths in Group Policy or set LongPathsEnabled to 1 in the registry, then restart.

How do I find every node_modules folder?

Use Get-ChildItem with -Recurse -Directory -Filter node_modules, excluding nested matches so each project is counted once by its outermost folder.

Does checking out projects near the drive root help?

Yes. A short base path leaves more room before the 260 character limit, which avoids most path errors on Windows before they happen.

Read next