How to install and cache Cypress in CI?
Cypress consists of an npm package and a separate binary that lands in a global cache (on Linux ~/.cache/Cypress), not in node_modules. So in CI the binary folder is cached with a key based on the lock file, and dependencies are installed with npm ci. If the install was skipped, cypress install adds the binary and cypress verify checks that it runs.
Cypress · CI
node_modules is cached, yet every run still downloads Cypress. What exactly has to be cached?
The CI logs of every run show Cypress being downloaded although "the cache is set up". This is a typical mix-up: Cypress consists of two parts, and the cache is usually set up for the other one. The lesson sorts out what lies where, what to cache and what to do when the binary is missing.
How it works and why. Cypress consists of the cypress npm package and a binary (the application itself with the browser engine) that the package downloads during installation. The package lives in node_modules, and the binary in the user's global cache: on Linux it is ~/.cache/Cypress. The binary is not an npm package, so it lands neither in node_modules nor in the npm cache ~/.npm. The package's postinstall hook downloads the binary already during npm ci, and the binary is missing only when the hook was skipped and nothing was cached. So in CI you cache the binary folder, and not node_modules.
Three layers, three different caches
- Dependencies.
npm ciinstalls packages exactly bypackage-lock.jsonand fails if the lock file disagrees withpackage.json, so in CI it is more reliable thannpm install.cypress-io/github-actionpicks the command by lock file itself. - The binary. Cache
~/.cache/Cypress, otherwise it is downloaded in every run. Thecypress-io/github-actionaction caches the binary and, for npm and Yarn Classic, the package manager cache automatically. - Another cache location. The
CYPRESS_CACHE_FOLDERvariable sets the folder. It is needed in GitLab, where only paths inside the project can be cached, and in containers with a non-standardHOME.
Example: from a download in every run to a cache. Without a cache npm ci downloads the binary (hundreds of megabytes) every time and adds time to the run that has nothing to do with the tests. A manual cache in Actions looks like this:
- uses: actions/cache@v6
with:
path: ~/.cache/Cypress
key: cypress-binary-${{ runner.os }}-${{ hashFiles('package-lock.json') }}
- run: npm ci
The key depends on the lock file: updating Cypress changes the lock file, the key becomes new and a new version is downloaded. The cache step stands before npm ci: then postinstall sees a ready binary and does not download it a second time. Official Cypress advice: do not cache node_modules between builds and do not restore the cache with a key that is too loose (restore-keys with no tie to the version), because the cache then accumulates binaries of every version ever installed.
Limits and alternatives. The binary may be missing when the postinstall hook was skipped (for example by --ignore-scripts) and the cache is empty. Then the error at startup says Cypress is not installed, and diagnosis is simple:
npx cypress cache path # where the binary cache lives
npx cypress cache list # which versions are installed
npx cypress install # download the binary if it is missing
npx cypress verify # check that Cypress starts
cypress install downloads the binary of the right version and does nothing if it is already there, and cypress verify checks that Cypress starts. If the binary does not need to be downloaded (for example the image already has it), CYPRESS_INSTALL_BINARY can be set to 0. An alternative to caching: a ready Docker image with the binary inside (see the lesson on images), where there is no download at all.
Typical mistakes.
- Caching
node_modulesand assuming the binary is cached too: it lies in another place. - Taking a cache by a key without the lock file or with a too loose
restore-keys: old binaries pile up and the version may not match the package. - Installing dependencies with
--ignore-scriptsand forgettingcypress install: the binary is missing. - Using
npm installinstead ofnpm ci: the build depends on what changed on the registry.
Check yourself
The workflow caches node_modules but every run downloads Cypress. Why and what do you change?
Answer
The Cypress binary lies outside node_modules, in ~/.cache/Cypress, and is not part of the npm cache. Cache the binary folder with a key from the lock file (or use cypress-io/github-action).
After npm ci --ignore-scripts Cypress does not start because the binary is missing. Which command fixes it and why is it safe to repeat?
Answer
npx cypress install: it downloads the binary of the right version and does nothing if it is already there. Then npx cypress verify checks that it starts.