cypress-io / github-action

GitHub Action for running Cypress end-to-end & component tests
https://on.cypress.io/guides/continuous-integration/github-actions
MIT License
1.35k stars 357 forks source link

Receiving `Error: Cannot find module 'cypress'` error in GitHub actions #1210

Closed brendanHearX closed 2 months ago

brendanHearX commented 2 months ago

Issue:

Seeing Error: Cannot find module 'cypress' error in GitHub actions logs on Cypress run step:

Run cypress-io/github-action@v6
  with:
    record: true
    install: false
    parallel: true
    group: UI-Chrome
    start: npm run start
    publish-summary: true
    component: false
Skipping install because install parameter is false
start server command "npm run start"
current working directory "/home/runner/work/website/website"
Error: Cannot find module 'cypress'

Workflow file:

name: Run Cypress Tests on Pull Request

on:
  push

jobs:
  install:
    runs-on: ubuntu-22.04
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Cypress install
        uses: cypress-io/github-action@v6
        with:
          runTests: false
          build: npm run build-ssr-staging

      - name: Save build folder
        uses: actions/upload-artifact@v4
        with:
          name: build
          if-no-files-found: error
          path: .next

  cypress-run:
    runs-on: ubuntu-22.04
    needs: install
    strategy:
      fail-fast: false
      matrix:
        containers: [1, 2]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download the build folder
        uses: actions/download-artifact@v4
        with:
          name: build

      - uses: actions/setup-node@v4
        with:
          node-version: '20.9.0'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Cypress run
        uses: cypress-io/github-action@v6
        with:
          record: true
          install: false
          parallel: true
          group: 'UI-Chrome'
          start: npm run start

Folder structure:

.
├── LICENSE.md
├── README.md
├── cypress
│   ├── downloads
│   ├── e2e
│   ├── screenshots
│   ├── support
│   ├── tsconfig.json
│   └── videos
├── cypress.config.ts
├── next.config.js
├── package-lock.json
├── package.json
├── prettier.config.js
├── public
├── src
└── tsconfig.json

package.json:

 "devDependencies": {
    ...
    "cypress": "^13.7.2",
    "typescript": "^4.9.5",
  }
MikeMcC399 commented 2 months ago

@brendanHearX

This may be a problem with your caching. You have replaced the installation and caching otherwise done by cypress-io/github-action.

In the job cypress-run you are using npm caching from actions/setup-node and then npm ci. This does not cache the Cypress binary. You can try adding npx cypress install after your npm ci. This should install the Cypress binary if it is missing.

brendanHearX commented 2 months ago

Thank you for the suggestion and explanation @MikeMcC399.

I tried to install cypress as suggested, but the same error is appearing.

Install Cypress

Run npx cypress@13.7.2 install --force
npm WARN exec The following package was not found and will be installed: cypress@13.7.2

Cypress 13.7.2 is installed in /home/runner/.cache/Cypress/13.7.2

Installing Cypress (version: 13.7.2)

[STARTED] Task without title.
[SUCCESS] Task without title.
[STARTED] Task without title.
[SUCCESS] Task without title.
[STARTED] Task without title.
[SUCCESS] Task without title.

You can now open Cypress by running: node_modules/.bin/cypress open

https://on.cypress.io/installing-cypress

Cypress run

Run cypress-io/github-action@v[6](https://github.com/hearSmart/website-lexie/actions/runs/9756023470/job/26925712499?pr=2748#step:7:7)
Skipping install because install parameter is false
Error: Cannot find module 'cypress'
Require stack:
- /home/runner/work/_actions/cypress-io/github-action/v6/dist/index.js

Updated cypress-run job:

  cypress-run:
    runs-on: ubuntu-latest
    needs: install
    strategy:
      fail-fast: false
      matrix:
        containers: [1, 2]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download the build folder
        uses: actions/download-artifact@v4
        with:
          name: build

      - uses: actions/setup-node@v4
        with:
          node-version: '20.9.0'
          cache: 'npm'

      - name: Install dependencies
        run: npm ci

      - name: Install Cypress
        run: npx cypress@13.7.2 install --force

      - name: Cypress run
        uses: cypress-io/github-action@v6
        with:
          install: false
          record: true
          parallel: true
          group: 'UI-Chrome'

Screenshot: Screenshot 2024-07-02 at 9 03 22 AM

MikeMcC399 commented 2 months ago

@brendanHearX

Is this a private project or is it publicly viewable?

brendanHearX commented 2 months ago

@MikeMcC399 It is a private project

MikeMcC399 commented 2 months ago

@brendanHearX

Normally the action will install dependencies for you. Is there some reason you are trying to do it by hand?

MikeMcC399 commented 2 months ago

@brendanHearX

If you want to continue installing dependencies by hand, I would add debug lines

npm ls cypress # to check what version of Cypress you are using
npx cypress verify # to check that everything is installed correctly

also change

npx cypress@13.7.2 install --force # to just
npx cypress install

Your package.json specifies:

"cypress": "^13.7.2",

so depending on when you ran the npm installation, you may get exactly cypress@13.7.2 or a later version like cypress@13.13.0which satisfies the semverrequirements.

If you want to use the installation capabilities of the action, then remove the lines:

        cache: 'npm'
        run: npm ci
        run: npx cypress@13.7.2 install --force
        install: false

Apart from that you may find that enabling debugging is in the action is helpful.

If you feel this is a bug in cypress-io/github-action, then please post a full debug log using

  env:
    DEBUG: '@cypress/github-action'
brendanHearX commented 2 months ago

Thanks again @MikeMcC399 for the support in the debugging and resolving the issue.

The only reason I tried to manually install dependencies was because I was trying to resolve the Cannot find module 'cypress' error.

I have found and resolved the issue. 🙏 🎉

The problem was in the Download the build folder step.

Next.js stores it's builds in a .next folder by default. The download step was taking the contents of that .next folder and placing it in the root directory, instead of keep the contents within the .next folder.

This caused issues because one of the files in the .next folder is an almost empty package.json file, which was overwriting the original root package.json file.

I think this resulted in the install process not finding the cypress dependency to install, and the start script to run, due to the wrong package.json existing in the root directory.

I just had to update the download step to store the build files in the .next folder by specifying the path, i.e.

      - name: Download the build folder
        uses: actions/download-artifact@v4
        with:
          name: build
          path: .next # ADDED THIS LINE

The complete workflow file is as follows and now working:

name: Run Cypress Tests on Pull Request

on: pull_request

jobs:
  install:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Cypress install
        uses: cypress-io/github-action@v6
        with:
          runTests: false
          build: npm run build-ssr-staging

      - name: Save build folder
        uses: actions/upload-artifact@v4
        with:
          name: build
          if-no-files-found: error
          path: .next

  cypress-run:
    runs-on: ubuntu-latest
    needs: install
    strategy:
      fail-fast: false
      matrix:
        containers: [1, 2]
    steps:
      - name: Checkout
        uses: actions/checkout@v4

      - name: Download the build folder
        uses: actions/download-artifact@v4
        with:
          name: build
          path: .next

      - name: Cypress run
        uses: cypress-io/github-action@v6
        with:
          record: true
          parallel: true
          group: 'UI'
          start: npm start
        env:
          GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
          CYPRESS_RECORD_KEY: ${{ secrets.CYPRESS_RECORD_KEY }}
MikeMcC399 commented 2 months ago

Congratulations on finding your problem @brendanHearX ! 🎉

It sounds like we can now close this issue.