使用CircleCI部署Gatsby至Firebase时Firebase命令找不到的问题排查
Let's break down why you're running into this error and fix it—no prior DevOps experience required!
What's Causing the Error?
That exit status 127 means CircleCI can't find the Firebase CLI executable in your node_modules/.bin directory. This usually happens for one of two reasons:
- Firebase CLI isn't installed as a project dependency: If you only installed it globally on your local machine, the CircleCI container won't have it.
- Workspace path misalignment: Your
attach_workspacestep in the deploy job might be pointing to the wrong directory, so thenode_modulesfrom the build job isn't accessible where you expect it.
Step-by-Step Fixes
1. Add Firebase CLI to Your Project Dependencies
First, make sure firebase-tools is listed in your package.json (this ensures it gets installed in node_modules during the build step). Run this locally and commit the changes:
npm install firebase-tools --save-dev
This adds Firebase CLI to your dev dependencies, so the npm install step in CircleCI will pull it into the container's node_modules folder.
2. Clean Up Your CircleCI Config
Your current config has redundant persist_to_workspace steps and a potential path mismatch in the deploy job. Here's the revised version with explanations:
# CircleCI Firebase Deployment Config version: 2 jobs: build: docker: - image: circleci/node:10 working_directory: ~/gatsby-site steps: - checkout - restore_cache: keys: - v1-npm-deps-{{ checksum "package-lock.json" }} - v1-npm-deps- - run: name: Install Dependencies command: npm install - save_cache: key: v1-npm-deps-{{ checksum "package-lock.json" }} paths: - ./node_modules - run: name: Gatsby Build command: npm run build # Combine persist steps into one (no need to do it twice) - persist_to_workspace: root: ~/gatsby-site paths: - . # Persist everything in the build directory deploy-production: docker: - image: circleci/node:10 working_directory: ~/gatsby-site # Match the build job's working directory steps: - attach_workspace: at: ~/gatsby-site # Attach workspace to the same directory as build - run: name: Firebase Deploy # Use npx to auto-locate the Firebase CLI (avoids path issues) command: npx firebase deploy --token "$FIREBASE_TOKEN" workflows: version: 2 build-and-deploy: jobs: - build - deploy-production: requires: - build
Key changes made:
- Combined
persist_to_workspace: You don't need to persist the workspace twice—doing it once after build ensures all files (includingnode_modulesand the built Gatsby site) are saved. - Aligned working directories: The deploy job uses the same
working_directoryas the build job, so the attached workspace lands exactly where it needs to be. - Switched to
npx: Instead of hardcoding the path to./node_modules/.bin/firebase,npxautomatically looks for the Firebase CLI in your project'snode_modulesfolder, eliminating path-related errors.
3. Verify Your Environment Variable
Double-check that FIREBASE_TOKEN is correctly set in your CircleCI project settings (under Project Settings > Environment Variables). This token lets CircleCI authenticate with Firebase, so it's critical for deployment.
Test the Fix
Commit your updated package.json, package-lock.json, and .circleci/config.yml to your repo, then trigger a new CircleCI build. The error should be gone, and your Gatsby site should deploy to Firebase successfully!
内容的提问来源于stack exchange,提问作者user2389087




