Gitlab CI react-scripts: not found

Viewed 2140

I'm trying to build a simple pipeline for a sample react project. This is my .gitlab-ci.yml file

image: node:12

stages:
  - build
  - test

build_react:
  stage: build
  script:
    - echo "Building deploy package"
    - yarn install
    - yarn build
    - echo "Build successful"
  artifacts:
    expire_in: 1 hour
    paths:
      - build

test_react:
  stage: test
  needs: [build_react]
  script:
    - echo "Testing project"
    - yarn test --watchAll=false
    - echo "Test successful"

The build passed, but on testing stage it failed complaining about

 $ react-scripts test --watchAll=false
 /bin/sh: 1: react-scripts: not found

enter image description here

1 Answers

It seems the problem comes from build_react:
Command yarn install will install the dependencies in the node_modules folder (This folder is not in the repository's committed files since it is mentioned in .gitignore).
If you need the dependencies in other dependent jobs (test_react in your case), they should be specified as cache or artifacts.
Thus, build_react might look like this:

⋮

build_react:
  stage: build
  script:
    - echo "Building deploy package"
    - yarn install
    - yarn build
    - echo "Build successful"
  artifacts:
    expire_in: 1 hour
    paths:
      - build
      - node_modules/    ### This will make the modules available to other dependent jobs

⋮
Related