Guides

How to delete node_modules and get space back

Every JavaScript or TypeScript project installs its dependencies in its own node_modules folder. One can weigh hundreds of MB, and with dozens of old projects you end up with several GB you don’t use.

It’s safe to delete. node_modules is rebuilt from package.json and the lockfile (package-lock.json, pnpm-lock.yaml or yarn.lock). If you go back to a project, just install again.

Find every node_modules folder

This command searches your home folder and prints the size of each one, without descending into nested ones:

find ~ -type d -name node_modules -prune -exec du -sh {} + 2>/dev/null | sort -h

The biggest ones end up at the bottom. It can take a few minutes if you have a lot of files.

Delete them

For one project, move its node_modules to the Trash from Finder or, in Terminal:

rm -rf path/to/project/node_modules

If you’d rather pick from a list, npkill shows every folder with its size and lets you delete them one by one:

npx npkill

Don’t forget the package manager caches

Besides node_modules, each package manager keeps a global cache:

To work on a project again

Go into the folder and run the install command for the package manager the project uses (npm install, pnpm install or yarn). The lockfile makes sure the same versions get installed.

If you also build for Apple platforms, see Can you delete Xcode’s DerivedData?.