18

我有一个 Nx monorepo ( https://nx.dev )。它有一个带有 Nx 缓存 ( ./node_modules/.cache/nx/) 的文件夹。它现在的大小超过 3GB。

是否有清除此缓存的命令?

4

5 回答 5

30

只需删除整个“nx”缓存文件夹:

rm -rf ./node_modules/.cache/nx
于 2021-03-31T17:31:27.630 回答
4

这适用于今天(2022 年 2 月 12 日)的最新版本。我不确定为什么这不再出现在 CLI 文档中,尽管有证据表明它过去存在:https ://nx.dev/cli/clear-cache

nx clear-cache

于 2022-02-12T15:16:37.280 回答
4

我已经实现了这样的解决方案,但觉得不方便。也许NX有清除缓存的命令,但我没有找到。

包.json

  "scripts": {
    "nx": "nx",
    "postnx": "node checkAndClearCache.js",
  ...

checkAndClearCache.js

const fs = require('fs');
const rimraf = require('rimraf');
const getSize = require('get-folder-size');

const cachePath = 'node_modules/.cache/nx';
const maxCacheMb = 2048;

if (fs.existsSync(cachePath)) {
  getSize(cachePath, (err, size) => {
    if (err) {
      throw err;
    }

    const MBSize = (size / 1024 / 1024).toFixed(2);

    console.log(`*** NX cache size is ${MBSize} Megabytes`);
    if (MBSize > maxCacheMb) {
      console.log('*** CLEAR NX CACHE ***');
      rimraf.sync(cachePath);
    }
  });
}
于 2021-01-27T09:19:51.107 回答
1

nx reset清除缓存。此处的文档:https ://nx.dev/using-nx/caching#local-computation-caching

上面的答案nx clear-cache是针对开玩笑的缓存。我会发表评论但没有代表:)

于 2022-02-22T19:08:12.853 回答
1

除了跳过它之外,实际上没有任何命令可以删除 Nx 缓存。如果目录的大小是您的问题,那么可能将您的节点脚本作为 cron 作业运行可能是一个选项。如果目录的位置是您关心的问题,那么您还可以对其进行配置并将其移到外部,node_modules 如下所示

于 2021-03-09T15:54:01.220 回答