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

使用Serverless GitHub Action时遭遇“serverless not found”错误求助

Fixing "serverless not found" Error with Serverless GitHub Action

When using the Serverless GitHub Action with multiple plugins, following the plugin usage guidance leads to a /bin/sh: 1: serverless: not found error—even after adding serverless as a dependency in package.json.

Your original workflow configuration:

name: Deploy main branch

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [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 }}
    - run: npm ci
    - name: serverless deploy
      uses: serverless/github-action@v3.2
      with:
        args: -c "cd backend/endpoints && \
                  serverless plugin install -n serverless-deployment-bucket && \
                  serverless plugin install -n serverless-python-requirements && \
                  serverless plugin install -n serverless-iam-roles-per-function && \
                  serverless plugin install -n serverless-offline && \
                  serverless deploy"
        entrypoint: /bin/sh
      env:
        SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}

Root Cause

Overriding the action's entrypoint to /bin/sh breaks access to the Serverless CLI provided by the action itself. Additionally, the locally installed serverless (via npm ci) isn't in the shell's PATH when using this custom entrypoint, so the command can't be located.

Solutions

Solution 1: Use Locally Installed Serverless with npx

Modify the args to use npx to invoke the locally installed Serverless CLI (available after npm ci runs):

- name: serverless deploy
  uses: serverless/github-action@v3.2
  with:
    args: -c "cd backend/endpoints && \
              npx serverless plugin install -n serverless-deployment-bucket && \
              npx serverless plugin install -n serverless-python-requirements && \
              npx serverless plugin install -n serverless-iam-roles-per-function && \
              npx serverless plugin install -n serverless-offline && \
              npx serverless deploy"
    entrypoint: /bin/sh
  env:
    SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}

npx automatically finds the serverless binary in node_modules/.bin without needing to adjust PATH.

Solution 2: Avoid Overriding Entrypoint (Recommended)

Split plugin installation into a separate step and let the action handle the Serverless CLI invocation directly. This aligns with the action's intended usage and is cleaner:

name: Deploy main branch

on:
  push:
    branches:
      - main

jobs:
  deploy:
    name: deploy
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [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 }}
    - run: npm ci
    # Install Serverless plugins in a dedicated step
    - name: Install Serverless plugins
      run: |
        serverless plugin install -n serverless-deployment-bucket
        serverless plugin install -n serverless-python-requirements
        serverless plugin install -n serverless-iam-roles-per-function
        serverless plugin install -n serverless-offline
      working-directory: backend/endpoints
      env:
        SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}
    # Use the action's default entrypoint for deployment
    - name: Serverless deploy
      uses: serverless/github-action@v3.2
      with:
        args: deploy
      working-directory: backend/endpoints
      env:
        SERVERLESS_ACCESS_KEY: ${{ secrets.SERVERLESS_ACCESS_KEY }}

This approach uses the Serverless CLI provided by the action, though keeping serverless in package.json is still recommended for consistency.

Package.json Configuration

Ensure your package.json includes serverless as a dependency (either in dependencies or devDependencies):

{
  "devDependencies": {
    "serverless": "^3.38.0"
  }
}

Run npm install serverless --save-dev locally to update your package.json before pushing changes.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 21:00:33