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

GitHub Action中NestJS项目构建报错:sh: 1: nest: Permission denied

GitHub Action中NestJS构建权限拒绝问题(sh: 1: nest: Permission denied)

针对这个问题,试试下面这些解决办法:

  • 改用npx调用Nest CLI
    本地环境可能全局安装了nest,但GitHub Action环境默认没有。把项目package.json里的build命令改成npx nest build,确保直接调用项目依赖内的nest CLI,而非全局命令。示例:

    "scripts": {
      "build": "npx nest build"
    }
    
  • 调整pnpm安装参数
    在GitHub Action的依赖安装步骤中,添加--frozen-lockfile参数,保证依赖安装与本地完全一致,避免权限异常:

    pnpm i --frozen-lockfile
    

    如果存在依赖提升相关问题,可再加--shamefully-hoist参数尝试:

    pnpm i --frozen-lockfile --shamefully-hoist
    
  • 手动修复可执行权限
    部分场景下,pnpm在GitHub Action环境安装的nest CLI会丢失可执行权限,可在构建前添加一步修复:

    - name: Fix nest CLI executable permissions
      run: chmod +x node_modules/.bin/nest
    
  • 检查Turborepo配置
    确认turbo.json中的build任务依赖关系正确,保证每个子包都能正常访问到nest CLI。示例pipeline配置:

    {
      "pipeline": {
        "build": {
          "dependsOn": ["^build"],
          "outputs": ["dist/**"]
        }
      }
    }
    

修改后的完整Action配置参考:

name: Test, lint and build
on: 
  push:
jobs:
  test-lint-build:
    runs-on: ubuntu-latest
    services:
      postgres:
        image: postgres
        env:
          POSTGRES_HOST: localhost
          POSTGRES_USER: test
          POSTGRES_PASSWORD: docker
          POSTGRES_DB: financing-database
        ports:
          - 2345:5432
        options: >-
          --health-cmd pg_isready
          --health-interval 10s
          --health-timeout 5s
          --health-retries 5
    steps:
      - name: Checkout
        uses: actions/checkout@v3

      - name: Install pnpm
        uses: pnpm/action-setup@v2.2.2
        with:
          version: latest

      - name: Install dependencies
        run: pnpm i --frozen-lockfile

      - name: Fix nest CLI permissions
        run: chmod +x node_modules/.bin/nest

      - name: Lint
        run: pnpm run lint

      - name: Test
        run: pnpm run test

      - name: Build
        run: pnpm run build
        env:
          VITE_SERVER_ENDPOINT: http://localhost:8000/api

      - name: Test financing-server (e2e)
        run: pnpm --filter @project/financing-server run test:e2e 

内容的提问来源于stack exchange,提问作者Kai Wißler

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 21:18:17