question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Cypress 10] Fail to compile in CI - Cannot find module 'cypress'

See original GitHub issue

Current behavior

When upgrading to Cypress 10, Cypress is installed in the dev dependencies.

  • The upgrade first caused an eslint error, not sure if this was expected… adding the rule to .eslintrc fixed the issue.

  • When running in CI, our build is trying to import cypress from cypress.config.ts but since dev-dependencies are not installed during production build, it caused the build to fail in BuildKite image

The workaround we have now is to remove ENV NODE_ENV=production in Dockerfile and that seems to solve the issue, but I’m not sure if it is the best solution as now cypress is included in the production node_module file and it has increased our docker image size too…

The below screenshot is from dive, it allows you to inspect image layer content image

Debug logs

Dockerfile

FROM node:lts-alpine AS deps
WORKDIR /opt/app
ENV CI=true
# comment out production node env
# ENV NODE_ENV=production
COPY package.json yarn.lock ./
RUN yarn config set no-progress && yarn --frozen-lockfile

FROM node:lts-alpine AS builder
WORKDIR /opt/app
COPY . .
COPY --from=deps /opt/app/node_modules ./node_modules
ENV NODE_ENV=production
RUN yarn build

FROM node:lts-alpine AS runner
WORKDIR /opt/app
ENV NODE_ENV=production
COPY --from=builder /opt/app/next.config.js ./
COPY --from=builder /opt/app/public ./public
COPY --from=builder /opt/app/.next ./.next
COPY --from=deps /opt/app/node_modules ./node_modules
CMD ["node_modules/.bin/next", "start"]

cypress.config.ts

import { defineConfig } from 'cypress';

export default defineConfig({
  projectId: ******,
  pageLoadTimeout: 80000,
  chromeWebSecurity: false,
  defaultCommandTimeout: 30000,
  requestTimeout: 30000,
  responseTimeout: 30000,
  viewportWidth: 1280,
  viewportHeight: 800,
  retries: {
    runMode: 2,
    openMode: 0
  },
  scrollBehavior: 'center',
  e2e: {
    setupNodeEvents(on, config) {
      // eslint-disable-next-line @typescript-eslint/no-var-requires
      require('cypress-fail-fast/plugin')(on, config);
      return config;
    },
    baseUrl: 'http://localhost:3000',
    specPattern: 'cypress/e2e/**/*.{js,jsx,ts,tsx}'
  }
});

Cypress Version

10.3.1

Package Manager

yarn

Operating system

Mac

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
aichichangcommented, Aug 4, 2022

Hey @AtofStryker, thank you for the advice! The problem is indeed from the Next.js tsconfig.json file. Adding cypress.config.ts in tsconfig.json - excludes and .eslintignore fixed the issue. Happy to close the issue, thanks again 😃

0reactions
AtofStrykercommented, Aug 4, 2022

@aichichang this is starting to make more sense. It almost looks like the tsconfig for your next.js project is type checking the cypress.config.ts. What does your tsconfig.json look like? Have you included the cypress types?

To possibly speed up cause identification, are you able to include a reproduction repo?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: Cannot find module 'cypress' · Issue #248 - GitHub
We just attempted to update to v2 and are now getting the following error when running Cypress. Here is our action config: jobs: ......
Read more >
Troubleshooting | Cypress Documentation
If Cypress cannot find a browser but you know you have it installed, there are ways to ensure that Cypress can "see" it....
Read more >
cypress-io/cypress - Gitter
tests/cypress/support/index.js Module build failed (from ... when running cypress the runner starts, but the test fails with "Cannot find module X" ...
Read more >
Cypress Github Action Fail - Stack Overflow
You need to put a first step to setup Node version. - name: Setup Node uses: actions/setup-node@v1 with: node-version: 15.
Read more >
@cypress/github-action - npm
If you re-run the GitHub workflow, if you use the same custom build id during recording, Cypress Cloud will cancel the run with...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found