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

GitHub Actions子目录运行报错:找不到依赖锁文件,求解决方案

解决GitHub Action在子目录执行Node.js测试的问题

问题原因

你设置的defaults.run.working-directory仅对run类型的步骤生效,对uses引入的第三方Action(比如actions/setup-node@v3)不生效。因此setup-node依然会在仓库根目录寻找依赖锁文件,但你的package-lock.json(或yarn.lock)实际在front-app/目录下,这就导致了找不到锁文件的错误。

解决方案

给actions/setup-node添加cache-dependency-path参数,明确指定锁文件的路径,让它能正确定位到子目录里的依赖锁文件。

修改后的完整Workflow配置如下:

# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions

name: Tests for front-app

on:
  push:
    branches: [ "main" ]
    paths: ["front-app/**"]
  pull_request:
    branches: [ "main" ]
    paths: ["front-app/**"]

defaults:
  run:
    working-directory: ./front-app/

jobs:
  test-front-app:

    runs-on: ubuntu-latest

    strategy:
      matrix:
        node-version: [16.x, 18.x]

    steps:
    - uses: actions/checkout@v3
    - name: Use Node.js ${{ matrix.node-version }}
      uses: actions/setup-node@v3
      with:
        node-version: ${{ matrix.node-version }}
        cache: 'npm'
        # 指定子目录中的依赖锁文件路径
        cache-dependency-path: front-app/package-lock.json
    - run: npm ci
    - run: npm run build --if-present
    - run: npm test

额外说明

  • 如果你的项目使用Yarn管理依赖,只需把cache字段改为yarn,cache-dependency-path改为front-app/yarn.lock即可。
  • 你之前配置的defaults.run.working-directory已经正确生效,npm ci、npm build等run步骤会自动在front-app/目录下执行,无需额外添加cd front-app && ...这类命令。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 06:54:07