如何清除 Nx 缓存

Pax*_*ach 5 javascript nomachine-nx nrwl nrwl-nx nx.dev

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

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

Cob*_*ear 96

nx reset清除缓存。

文档:https nx reset: //nx.dev/nx/reset#reset

有关缓存的文档:https ://nx.dev/using-nx/caching#local-computation-caching

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


meg*_*lop 77

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

rm -rf ./node_modules/.cache/nx
Run Code Online (Sandbox Code Playgroud)

  • 我的一些测试有时会被卡住,这是唯一能让事情再次正常运行的方法 (2认同)

Joe*_*ung 35

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

nx clear-cache

  • `nx help` 打印:`nx reset - 清除所有缓存的 Nx 工件 (...) [别名:clear-cache]`。但 CLI 文档中也没有“reset”。 (2认同)

小智 11

除了跳过它或使用以下命令之外,实际上没有任何命令可以删除 Nx 缓存。

npx nx run build --skip-nx-cache

npx nx run test --skip-nx-cache

如果目录的大小是您的问题,那么可以选择将节点脚本作为 cron 作业运行。如果您关心目录的位置,那么您还可以配置它并将其移到外部,node_modules 如下所示


Pax*_*ach 4

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

包.json

  "scripts": {
    "nx": "nx",
    "postnx": "node checkAndClearCache.js",
  ...
Run Code Online (Sandbox Code Playgroud)

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);
    }
  });
}
Run Code Online (Sandbox Code Playgroud)