You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CircleCI配置Yarn依赖缓存时执行restore_cache/save_cache操作报错的解决咨询

Fixing "yarn.lock: no such file or directory" Error in CircleCI Cache Configuration

Let's break down what's happening here and fix it step by step:

The Root Cause

The error message tells you CircleCI can't find /home/circleci/project/yarn.lock—and looking at your project structure, that makes total sense! Your yarn.lock (and package.json) live inside the frontend subdirectory, not the project root. When CircleCI runs checksum "yarn.lock", it's looking in the default working directory (/home/circleci/project) instead of frontend.

Fix 1: Update the Cache Key Path

First, adjust the checksum path in both restore_cache and save_cache to point to the correct location of yarn.lock:

  • Replace {{ checksum "yarn.lock" }} with {{ checksum "frontend/yarn.lock" }}

Fix 2: Correct the Yarn Install Working Directory

Your current run step sets working_directory: ./frontend/tests, but yarn install needs to run in the directory where package.json and yarn.lock are (the frontend folder). Running it from tests will cause yarn to look for a lockfile there, which doesn't exist.

Also, your --cache-folder flag has a typo (~/. instead of ~/.cache/yarn), which would point to the home directory instead of yarn's standard cache location.

Fixed Config.yml

Here's the revised configuration with all fixes applied:

version: 2.1
jobs:
  build-and-test-frontend:
    docker:
      - image: circleci/node:14
    steps:
      - checkout
      - restore_cache:
          name: Restore Yarn Package Cache
          keys:
            - yarn-packages-{{ checksum "frontend/yarn.lock" }} # Updated path
      - run:
          working_directory: ./frontend # Changed to frontend root
          name: Install Dependencies and Run Tests
          command: |
            yarn install --frozen-lockfile --cache-folder ~/.cache/yarn # Fixed cache path
            yarn test # Runs test script from frontend/package.json
      - save_cache:
          name: Save Yarn Package Cache
          key: yarn-packages-{{ checksum "frontend/yarn.lock" }} # Updated path
          paths:
            - ~/.cache/yarn
workflows:
  sample:
    jobs:
      - build-and-test-frontend

Why This Works

  • The checksum now targets the correct frontend/yarn.lock file, so CircleCI can generate a valid cache key.
  • Running yarn install from the frontend directory ensures yarn uses the local package.json and yarn.lock to install dependencies.
  • The corrected --cache-folder matches the path in save_cache, so yarn will store and retrieve cached packages from the same location.

内容的提问来源于stack exchange,提问作者mlan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 05:07:36