Angular 13项目运行触发[readCachedProjectGraph]缓存缺失错误求助
Angular 13 + Nx 运行报错:无可用的缓存ProjectGraph 解决方法
错误信息
[error] Error: [readCachedProjectGraph] ERROR: 无可用的缓存ProjectGraph。 如果您直接使用`readCachedProjectGraph()`,则需要重构代码,确保在读取数据前先调用`await createProjectGraphAsync()`来创建ProjectGraph。 如果您在运行标准nx命令时遇到此错误,请提交issue到Nx官方仓库。 请确保在postinstall脚本中执行'node ./decorate-angular-cli.js',装饰后的CLI将计算项目图。执行'ng --help'应显示'Smart, Fast and Extensible Build System'。 at readCachedProjectGraph (C:\Project\xxxx\node_modules\nx\src\project-graph\project-graph.js:28:15) at calculateResolveMappings (C:\Project\xxxxx\node_modules\@nrwl\node\src\executors\node\node.impl.js:57:62) at nodeExecutor_1 (C:\Project\xxxxx\node_modules\@nrwl\node\src\executors\node\node.impl.js:34:26) at nodeExecutor_1.next (<anonymous>) at resume (C:\Project\xxxxx\node_modules\tslib\tslib.js:230:48) at C:\Project\xxxxx\node_modules\tslib\tslib.js:229:125 at new Promise (<anonymous>) at Object.i.<computed> [as next] (C:\Project\xxxxx\node_modules\tslib\tslib.js:229:67) at recurse (C:\Project\xxxxx\node_modules\@nrwl\devkit\src\utils\convert-nx-executor.js:44:22) at Observable._subscribe (C:\Project\xxxxx\node_modules\@nrwl\devkit\src\utils\convert-nx-executor.js:61:13)
解决步骤
检查并配置postinstall脚本
打开项目根目录的package.json,确认scripts节点下存在如下命令:"postinstall": "node ./decorate-angular-cli.js"如果没有,添加后执行
npm install或yarn install,触发脚本自动装饰Angular CLI。验证CLI装饰状态
在终端执行ng --help,如果输出内容包含Smart, Fast and Extensible Build System,说明CLI已被正确装饰;如果没有,手动执行装饰脚本:node ./decorate-angular-cli.js手动生成ProjectGraph缓存
执行以下命令生成并缓存ProjectGraph:nx graph完成后再重新运行你的应用启动命令。
清理旧缓存后重新生成
如果缓存存在异常,先清理Nx旧缓存:nx reset清理完成后再次执行
nx graph生成新的缓存。检查自定义代码中的ProjectGraph调用
如果你的项目代码中直接使用了readCachedProjectGraph(),必须在调用前先执行await createProjectGraphAsync()生成ProjectGraph,重构代码确保执行顺序正确。
内容的提问来源于stack exchange,提问作者Dr. Rahul Jha
相关产品推荐
相关产品推荐

