github-action icon indicating copy to clipboard operation
github-action copied to clipboard

Error "Cannot find module 'cypress'" using Yarn Berry

Open saseungmin opened this issue 4 years ago • 14 comments

Hi there! This action worked with yarn berry workspaces. but, this action can't find the "cypress" module.

Error: Cannot find module 'cypress'

I found the issue of Yarn Berry and tried as follows.

  • https://github.com/cypress-io/github-action/pull/260

here is the relavant configuration:

jobs:
  check-e2e-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x

      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          install-command : yarn install --immutable --immutable-cache
          start: yarn start
          wait-on: 'http://localhost:3000'
          browser: chrome
          headless: true

here is github-action log:

Run cypress-io/github-action@v2
  with:
    install-command: yarn install --immutable --immutable-cache
    build: yarn run build
    start: yarn start
    wait-on: http://localhost:3000
    browser: chrome
    headless: true
    record: false
    config-file: cypress.json
Received 22 of 22 (100.0%), 0.0 MBs/sec
Cache Size: ~0 MB (22 B)
/usr/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/be0c8559-6dda-42c1-822d-2db565488a1c/cache.tzst -P -C /home/runner/work/*****/*****
Cache restored successfully
Received 0 of 353031365 (0.0%), 0.0 MBs/sec
Received 58720256 of 353031365 (16.6%), 28.0 MBs/sec
Received 155189248 of 353031365 (44.0%), 49.3 MBs/sec
Received 251658240 of 353031365 (71.3%), 60.0 MBs/sec
Received 344642757 of 353031365 (97.6%), 65.7 MBs/sec
Received 353031365 of 353031365 (100.0%), 61.3 MBs/sec
Cache Size: ~337 MB (353031365 B)
/usr/bin/tar --use-compress-program zstd -d -xf /home/runner/work/_temp/dd9a16d5-2321-41d4-9f8f-3e8284d07e08/cache.tzst -P -C /home/runner/work/*****/*****
Cache restored successfully
install command command "yarn install --immutable --immutable-cache"
current working directory "/home/runner/work/*****/*****"
/usr/local/bin/yarn install --immutable --immutable-cache
➤ YN0000: ┌ Resolution step
Resolution step
➤ YN0000: └ Completed in 0s 376ms
➤ YN0000: ┌ Fetch step
Fetch step
➤ YN0000: └ Completed in 0s 743ms
➤ YN0000: ┌ Link step
Link step
➤ YN0000: └ Completed in 0s 397ms
➤ YN0000: Done with warnings in 1s 966ms
/opt/hostedtoolcache/node/14.17.6/x64/bin/npx cypress cache list
┌─────────┬───────────────────┐
│ version │ last used         │
├─────────┼───────────────────┤
│ 8.4.1   │ a few seconds ago │
├─────────┼───────────────────┤
│ 8.5.0   │ a few seconds ago │
└─────────┴───────────────────┘
build app command "yarn run build"
current working directory "/home/runner/work/*****/*****"
/usr/local/bin/yarn run build
The following changes are being made to your tsconfig.json file:
  - compilerOptions.paths must not be set (aliased imports are not supported)

Creating an optimized production build...
Compiled successfully.

File sizes after gzip:

  74.98 KB  build/static/js/2.c1fee284.chunk.js
  1.71 KB   build/static/js/3.b8a66e6b.chunk.js
  1.17 KB   build/static/js/runtime-main.2d193d74.js
  1.01 KB   build/static/js/main.a1fef262.chunk.js
  554 B     build/static/css/main.f09a1402.chunk.css

The project was built assuming it is hosted at /.
You can control this with the homepage field in your package.json.

The build folder is ready to be deployed.
You may serve it with a static server:

  yarn global add serve
  serve -s build

Find out more about deployment here:

  cra.link/deployment

start server "yarn start command "yarn start"
current working directory "/home/runner/work/*****/*****"
waiting on "http://localhost:3000" with timeout of 60 seconds
/usr/local/bin/yarn start
The following changes are being made to your tsconfig.json file:
  - compilerOptions.paths must not be set (aliased imports are not supported)

ℹ 「wds」: Project is running at http://10.1.0.5
ℹ 「wds」: webpack output is served from 
ℹ 「wds」: Content not from webpack is served from /home/runner/work/*****/*****/public
ℹ 「wds」: 404s will fallback to /
Starting the development server...

Compiled successfully!

You can now view ***** in the browser.

  Local:            http://localhost:3000
  On Your Network:  http://10.1.0.5:3000

Note that the development build is not optimized.
To create a production build, use yarn build.

Error: Cannot find module 'cypress'
Require stack:
- /home/runner/work/_actions/cypress-io/github-action/v2/dist/index.js

The above error occurred and was solved using with command.

  check-e2e-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x

      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          install-command : yarn install --immutable --immutable-cache
          command: yarn test:e2e

here is yarn test:e2e script:

"test:e2e": "start-server-and-test start http://localhost:3000 cypress:run",

Why can't I find the cypress module when I use the command provided by Cypress github-action? Or did I miss something? I'd appreciate it if you had any other advice. thank you!🙏

saseungmin avatar Sep 28 '21 13:09 saseungmin

I have this same issue. I don't run the install command in the cypress-io/github-action@v2, but in a previous workflow run step:

run: yarn install --immutable --immutable-cache

and then later:

      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          install: false
          start: yarn start:test
          wait-on: 'http://localhost:3000'
          wait-on-timeout: 200
          browser: chrome
          headless: true
          record: true

This was working previously with yarn, but this is now yarn "berry" (3.0.2).

I am using PnP and a Zero-Installs Yarn setup.

forgo avatar Sep 28 '21 20:09 forgo

@forgo Thank you for your advice! I modified it as you suggested, but it doesn't work.

  check-e2e-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x

      - name: Install dependencies
        run: yarn install --immutable --immutable-cache

      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
          install: false
          start: yarn start
          wait-on: 'http://localhost:3000'
          browser: chrome
          headless: true

I'm using PnP and a Zero-Installs Yarn 3.0.2 setup. Anyway, is it impossible to install using yarn berry in Cypress github-action?

saseungmin avatar Sep 29 '21 02:09 saseungmin

Same cannot get it to work. A timeout occurs during pipe run:

Opening Cypress...
[1965:1017/182222.115087:ERROR:node_bindings.cc(278)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[1965:1017/182222.115179:ERROR:node_bindings.cc(278)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[1965:1017/182224.237041:ERROR:bus.cc(392)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
[1965:1017/182224.237078:ERROR:bus.cc(392)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
[2138:1017/182224.258717:ERROR:gpu_init.cc(441)] Passthrough is not supported, GL is swiftshader
[1965:1017/182224.491841:ERROR:bus.cc(392)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")
[1965:1017/182224.516792:ERROR:bus.cc(392)] Failed to connect to the bus: Could not parse server address: Unknown address type (examples of valid types are "tcp" and on UNIX "unix")

.yml

name: CI
on: push

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2

    - name: Get yarn cache directory path
      id: yarn-cache-dir-path    
      run: echo "::set-output name=dir::$(yarn config get cacheFolder)"
      
    - uses: actions/cache@v2
      id: yarn-cache
      with:
        path: ${{ steps.yarn-cache-dir-path.outputs.dir }}
        key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
        restore-keys: |
          ${{ runner.os }}-yarn-

    - name: Yarn Install if no cache hit
      if: steps.yarn-cache.outputs.cache-hit != 'true'
      run: yarn add --cached

    - name: Eslint
      run: yarn lint
            
    - name: Run Component tests 🧪
      uses: cypress-io/github-action@v2
      with:
        install-command : yarn install --immutable --immutable-cache
        command: yarn cypress run-ct 

ZeroPie avatar Oct 17 '21 18:10 ZeroPie

The problem is here: https://github.com/cypress-io/github-action/blob/0c9eccfb5989bd87ea58b5d8cce9a4f0db1a6d67/index.js#L621

The Cypress action runs the above index.js file as a Node script. It does so using NPM.

require.resolve('cypress') throws an error in Yarn 2, because the index.js script is running using NPM, meaning the Plug N Play file (.pnp.cjs) is not loaded.

The index.js file ironically looks for Yarn PNP cache files, but it has no way of using files in that cache. 😂

To bypass this (for now), you can use command-prefix: yarrn dlx to pass the default cypress run command through Yarn to find it through PNP. The code path for command-prefix does not use require.resolve, but this feels like coincidence so I'd ask the repo owners still address this.

I believe to fix this formally, the GitHub action needs to load the .pnp.cjs file before or during executing index.js.

Again, for easy access:

Unblocking yourself

  • Add command-prefix: yarn dlx to the action's with.

Notes for repository owners

  • If I'm not mistaken, run your dist/index.js file with Yarn's PNP file (.pnp.cjs) so that require.resolve finds PNP modules like cypress.

quisido avatar Oct 22 '21 20:10 quisido

@CharlesStover Can I use the command provided by "cypress-github-actions" using command-refix: yarndlx?

github action ci example:

  check-e2e-test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Use Node.js 14.x
        uses: actions/setup-node@v1
        with:
          node-version: 14.x
      - name: Cypress run
        uses: cypress-io/github-action@v2
        with:
		  command-prefix: yarn dlx
          start: yarn start
          wait-on: 'http://localhost:3000'
          browser: chrome
          headless: true

saseungmin avatar Oct 23 '21 16:10 saseungmin

@CharlesStover Awesome!!👏 thanks you!👍

saseungmin avatar Oct 26 '21 01:10 saseungmin

@CharlesStover in my demo, the correct way is "command-prefix: yarn">_<

www159 avatar Nov 23 '21 13:11 www159

the wrong version

Run cypress-io/github-action@v2
/usr/local/bin/yarn --frozen-lockfile
yarn install v1.22.17
[1/4] Resolving packages...
[2/4] Fetching packages...
[3/4] Linking dependencies...
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "konva@>=2.6".
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "react-konva@^16.8.0  || ^17.0.0".
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "react-native@>=0.58".
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "@react-three/fiber@>=6.0".
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "three@>=0.126".
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "react-zdog@>=1.0".
warning "react-spring > @react-spring/[email protected]" has unmet peer dependency "zdog@>=1.0".
[4/4] Building fresh packages...
$ patch-package
patch-package 6.4.7
Applying patches...
@types/[email protected] ✔
@types/[email protected] ✔
@types/[email protected] ✔
Done in 31.35s.
/usr/local/bin/npx cypress cache list
┌─────────┬─────────────┐
│ version │ last used   │
├─────────┼─────────────┤
│ 9.0.0   │ 13 days ago │
└─────────┴─────────────┘
/usr/local/bin/npx cypress verify

[STARTED] Task without title.
[SUCCESS] Task without title.
/usr/bin/tar --posix --use-compress-program zstd -T0 -cf cache.tzst -P -C /home/runner/work/editor/editor --files-from manifest.txt
Cache Size: ~148 MB (155076066 B)
Cache saved successfully
/usr/bin/tar --posix --use-compress-program zstd -T0 -cf cache.tzst -P -C /home/runner/work/editor/editor --files-from manifest.txt
Cache Size: ~187 MB (196090537 B)
Cache saved successfully
start server "yarn vite command "yarn vite"
current working directory "/home/runner/work/editor/editor"
waiting on "http://localhost:3000" with timeout of 60 seconds
/usr/local/bin/yarn vite
yarn run v1.22.17
$ /home/runner/work/editor/editor/node_modules/.bin/vite
Pre-bundling dependencies:
  prosemirror-commands
  prosemirror-inputrules
  prosemirror-model
  prosemirror-state
  prosemirror-view
  (...and 6 more)
(this will be run only when your dependencies or config have changed)

  vite v2.6.14 dev server running at:

  > Local:    http://localhost:3000/
  > Network:  http://10.1.0.71:3000/

  ready in 768ms.

Cypress test command: npx yarn dlx cypress run --config-file cypress.json
/usr/local/bin/npx yarn dlx cypress run --config-file cypress.json
yarn run v1.22.17
error Command "dlx" not found.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Error: The process '/usr/local/bin/npx' failed with exit code 1

my yml

name: type simulate test
on: [push]
jobs:
  cypress-run:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Cypress-run
        uses: cypress-io/github-action@v2
        with:
          command-prefix: yarn
          start: yarn vite
          wait-on: 'http://localhost:3000'



www159 avatar Nov 23 '21 13:11 www159

I was really stuck and that saved me so much time, thanks @CharlesStover ! 🥇

kahboom avatar Mar 29 '22 17:03 kahboom

Has anybody had this work-around "break" for them w/the cypress 10 release today?

What happened is that now when the command runs w/prefix it downloads latest (v10), but then fails bc it's not compatible with Cypress 10 (yet)

Cypress test command: npx yarn dlx cypress run --headless --config baseURL=PATH,retries=2 --config-file cypress.json
/usr/local/bin/npx yarn dlx cypress run --headless --config baseUrl=PATH,retries=2 --config-file cypress.json
➤ YN0000: ┌ Resolution step
Resolution step
➤ YN0000: └ Completed in 1s 409ms
➤ YN0000: ┌ Fetch step
Fetch step
➤ YN0000: └ Completed in 3s 478ms
➤ YN0000: ┌ Link step
Link step
➤ YN0000: └ Completed in 1s 456ms
➤ YN0000: Done with warnings in 6s 3[69](https://github.com/launchdarkly/git-gatsby/runs/6693611844?check_suite_focus=true#step:3:74)ms

It looks like this is your first time using Cypress: 10.0.0

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

Opening Cypress...
[2312:0601/162832.292547:ERROR:node_bindings.cc(276)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[2312:0601/162832.292612:ERROR:node_bindings.cc(276)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[2312:0601/162832.292618:ERROR:node_bindings.cc(276)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[2312:0601/162832.292623:ERROR:node_bindings.cc(276)] Most NODE_OPTIONs are not supported in packaged apps. See documentation for more details.
[2464:0601/162833.4272[88](https://github.com/launchdarkly/git-gatsby/runs/6693611844?check_suite_focus=true#step:3:95):ERROR:sandbox_linux.cc(377)] InitializeSandbox() called with multiple threads in process gpu-process.
[2464:0601/162833.432306:ERROR:gpu_memory_buffer_support_x11.cc(44)] dri3 extension not supported.
Could not find a Cypress configuration file.

We looked but did not find a cypress.json file in this folder: /home/runner/work/git-gatsby/git-gatsby
Error: The process '/usr/local/bin/npx' failed with exit code 1

I tried up two things:

  1. upgrade to 10 (runs locally)
smoke-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Cypress run
        uses: cypress-io/github-action@v4
        with:
          command-prefix: yarn dlx
          install-command: yarn install --immutable
          headless: true
          config: baseUrl=PATH,retries=2
  1. Keep using yarn v9, but use a custom command instead of custom-prefix
smoke-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Cypress run
        uses: cypress-io/github-action@v4
        with:
          command: npx yarn dlx cypress@v9 run --headless  --config baseUrl=PATH,retries=2 --config-file cypress.json
          install-command: yarn install --immutable

Both options just result in Error: Cannot find module 'cypress'

jazanne avatar Jun 01 '22 19:06 jazanne

@jazanne This is a wildly mislabeled error on Cypress's part.

We looked but did not find a cypress.json file in this folder...

The real error, which is shown if you run cypress run locally instead of through the GitHub Action, is

There is a cypress.json file at the path: F:\git\charlesstover.com Cypress version 10.0.0 no longer supports cypress.json.

Rename cypress.json to cypress.config.ts and export default the object instead. Then update your GitHub Action's with to include config-file: cypress.config.ts.

You may have some small changes from there, like config file shape, but otherwise this was the only blocker for getting Cypress 10 to pass for me: example run

quisido avatar Jun 01 '22 23:06 quisido

@CharlesStover thanks for your recommendation.

I was able to get this working with Option 2 (stay on Cypress 9 for now) I described above with minor change of locking the github action to 3.1.0, because a code change in 3.2.0 resulted in the Error: Cannot find module 'cypress' error since module resolution is done before running custom command

smoke-test:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v3
      - name: Cypress run
        uses: cypress-io/[email protected] # lock version
        with:
          command: npx yarn dlx cypress@v9 run --headless  --config baseUrl=PATH,retries=2 --config-file cypress.json
          install-command: yarn install --immutable

jazanne avatar Jun 02 '22 14:06 jazanne

I've had success using:

  • Latest Cypress (10.3.0 as of writing this)
  • The new Cypress config format (cypress.config.js instead of cypress.json)
  • Latest Yarn (3.2.1 as of writing this)
  • cypress-io/[email protected]

Note that you will need to explicitly set the path to your Cypress config file, as otherwise the 3.1.0 action still looks for cypress.json.

Example action:

- name: Run Cypress with Chrome
  uses: cypress-io/[email protected]
  with:
    browser: chrome
    record: true
    config-file: cypress.config.js
    install-command: yarn install --immutable
    command-prefix: yarn dlx

pascal-giguere avatar Jul 07 '22 14:07 pascal-giguere

Has anybody got this to work with Cypress 10, Yarn 3, and cypress-io/github-action v4?

I can get it working with Cypress 10.8.0, Yarn 3.2.3, and cypress-io/github-action v3.1.0. But I would like to instead use cypress-io/github-action v4.

lenzi-e avatar Sep 21 '22 19:09 lenzi-e

I also cannot get it working with cypress-io/github-action v4.

PilotConway avatar Sep 23 '22 20:09 PilotConway

Cypress v10 + Cypress GitHub Action v4 + Yarn v3

Sorry I'm late to upgrading to v4. I managed this dirty workaround:

Before running the cypress-io/github-action, run the following:

  1. Setup Node
  2. yarn install
  3. yarn run cypress:pnp

In your package.json, create the script "cypres:pnp": "node ./scripts/cypress-pnp.cjs".

Create the file ./scripts/cypress-pnp.cjs. If you are using Yarn workspaces, create it in the workspace that's running cypress.

// running Cypress from root
const { mkdirSync, writeFileSync } = require('fs');

const SPACES = 2;

const PACKAGE_JSON = {
  name: 'cypress',
  version: '0.0.1',
  main: './index.cjs',
  type: 'commonjs',
  peerDependencies: {
    cypress: '*',
  },
};

mkdirSync('./node_modules/cypress', {
  recursive: true,
});

writeFileSync(
  './node_modules/cypress/package.json',
  JSON.stringify(PACKAGE_JSON, null, SPACES),
);

writeFileSync(
  './node_modules/cypress/index.cjs',
  `require('../../.pnp.cjs').setup();
module.exports = require('cypress');`,
);
// running Cypress from packages/package-name workspace:
const { mkdirSync, writeFileSync } = require('fs');

const SPACES = 2;

const PACKAGE_JSON = {
  name: 'cypress',
  version: '0.0.1',
  main: './index.cjs',
  type: 'commonjs',
  peerDependencies: {
    cypress: '*',
  },
};

mkdirSync('./node_modules/cypress', {
  recursive: true,
});

writeFileSync(
  './node_modules/cypress/package.json',
  JSON.stringify(PACKAGE_JSON, null, SPACES),
);

writeFileSync(
  './node_modules/cypress/index.cjs',
  `require('../../../../.pnp.cjs').setup();
module.exports = require('cypress');`,
);

(The only difference between the two is the ../../ before .pnp.cjs for workspaces.)

For the cypress-io/github-action:

  1. If you are using with.project for a workspace, move it to with.working-directory.
  2. Set with.install to false. Remove with.install-command since we installed in a previous step.
  3. Just to be safe, keep with.command-prefix set to yarn dlx.

If time permits this weekend, I'll package this script up into a Node module to make this easier; but the above copy-paste job should work for now.

Link to proof of concept run where the installation is passing for me.

quisido avatar Oct 29 '22 08:10 quisido

You're likely to get this error if you're using yarn or pnpm workspaces. To solve it, just install cypress before running this action, like so:

- run: pnpm install cypress -wD # or yarn add cypress -wD
- uses: cypress-io/[email protected]

nwaughachukwuma avatar Dec 14 '22 07:12 nwaughachukwuma

:tada: This issue has been resolved in version 5.7.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket:

github-actions[bot] avatar May 05 '23 14:05 github-actions[bot]

@saseungmin

Please check out Yarn Plug'n'Play and see if it meets your needs. This is new functionality contributed by @PilotConway.

MikeMcC399 avatar May 05 '23 15:05 MikeMcC399

@MikeMcC399 @PilotConway

Thank you for finally solving this issue! 🙇‍♂️ I checked that it works perfectly.

saseungmin avatar May 05 '23 15:05 saseungmin