Cleaning up the NPM cache

Quite a while ago I realized that in my local development machines and in some continuous integration servers, the available hard disk free space was suspiciously shrinking over time. A quick research showed that directories related to the npm package manager were guilty.

Just reviewing the npm documentation, you stumble first on the npm-cache command. Thus executing:

$ npm cache clean

should work. But that just cleans up the user cache directory, which by default is located at $HOME/.npm on POSIX-oriented operating systems, or at %AppData%/npm-cache on Windows systems. There are a bunch of temporary files stored by default in directories at:

  • $TMPDIR/npm-* on POSIX-oriented operating systems, or
  • %TMP%\npm-* (%TEMP%\npm-*) on Windows systems.

According to the npm-folders documentation:

Temp files are given a unique folder under this root for each run of the program, and are deleted upon successful exit.

However (at the moment of writing this post), it does not seem that this deletion happens. A temporary workaround is to run the command:

$ sudo rm -rf $TMPDIR/npm-*

And even better, let your computer run it automatically with a cron job.