.npm 下的缓存文件太多怎么办?.npm 下缓存的包长时间未清理,占用空间太大怎么办?
查看磁盘占用情况
linux 系统里,查看磁盘占用情况:df -h
-
查看单个目录磁盘占用情况
du -sh /指定目录 -
查看所有目录的磁盘占用情况
for i in `ls -a`;do du -sh $i;done
清除.npm 缓存文件
从上图我们可以看到,.npm 下的文件占用了18G 的容量。这个是我们测试服务器上的信息,由于有很长的时间都没有去做清理,所以缓存文件非常多。
直接把缓存文件干掉? 这样所有的依赖包都需要重新下载!
npm 针对缓存的操作提供了以下的方法(npm-cache):
-
npm cache add
<package-spec>- add: Add the specified packages to the local cache. This command is primarily intended to be used internally by npm, but it can provide a way to add data to the local installation cache explicitly.
-
npm cache clean
[<key>]- clean: Delete all data out of the cache folder. Note that this is typically unnecessary, as npm's cache is self-healing and resistant to data corruption issues.
-
npm cache ls
[<name>@<version>] -
npm cache verify
- verify: Verify the contents of the cache folder, garbage collecting any unneeded data, and verifying the integrity of the cache index and all cached data.
描述我没有做翻译,英语好的应该能够看得更清晰。
如上,我们可以使用npm cache clean --force 来清除所有的缓存信息,但是如上面的描述所说,这种操作并不是必要的。
我们也可以使用npm chache verify 来进行检查处理。它会检验缓存文件,回收掉任何不需要的文件。
清理结果
以下结果是二次运行 npm cache verify的输出:
我们看到,从一开始的18G,通过两次清理之后,剩下991M还有效的缓存数据。我们也不用非得把所有的缓存数据都删掉!!