使用GitHub Actions构建React项目时在build-for-devtools步骤出现cross-env: not found错误求助
Alright, let's tackle this error you're seeing. The root problem here is that the cross-env tool isn't available when your build-for-devtools script runs—and that's happening because your dependency installation step is running in the wrong directory.
Why This Happens
Your current workflow clones the React repo, but then runs yarn install in the main workspace folder, not inside the cloned react directory. That means all the dev dependencies (including cross-env) get installed in the wrong place, so when you later run the build script from the react folder, it can't find cross-env in its local node_modules.
Fix 1: Correct the Yarn Install Directory
The simplest fix is to navigate into the cloned react folder before running yarn install. Update your "clone source code" step like this:
- name: clone source code run: | git clone https://github.com/facebook/react.git cd react yarn install
This ensures all dependencies (including cross-env) are installed directly in the React repo's node_modules folder, where the build script expects them.
Fix 2: Use GitHub's Official Checkout Action (Recommended)
Instead of manually cloning with git clone, using the official actions/checkout action is more reliable and follows GitHub Actions best practices. It handles cloning the repo to the correct path automatically. Here's the adjusted workflow:
jobs: build: runs-on: ubuntu-18.04 steps: - name: Checkout React source code uses: actions/checkout@v4 with: repository: facebook/react path: react - name: setup node.js environment uses: actions/setup-node@v2.1.5 with: node-version: 14 - name: install yarn run: sudo npm install -g yarn - name: Install project dependencies run: | cd react yarn install - name: build-for-devtools run: | cd react yarn build-for-devtools
Debug Step (If Needed)
If you still run into issues, add a quick step to verify cross-env is installed correctly:
- name: Verify cross-env is installed run: | cd react yarn list cross-env
This will show you if cross-env is present in the project's dependencies, helping you confirm the install step worked.
When you run yarn install inside the react directory, Yarn automatically adds the local node_modules/.bin folder to the PATH for any subsequent yarn scripts. That's why the build script will be able to find cross-env once the dependencies are installed in the right place.
内容的提问来源于stack exchange,提问作者nobody

