You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

新手求助:GitLab流水线中Cypress npm包已装但二进制文件缺失

问题根源

你遇到的报错是因为GitLab CI缓存只保留了node_modules目录,但Cypress的二进制文件默认存在用户级缓存目录(比如~/.cache/Cypress),不在node_modules范围内。首次运行npm install时会自动下载二进制文件,但后续复用缓存的node_modules时,二进制文件没被缓存下来,就触发了缺失报错。

解决方案

方案1:调整缓存路径,包含Cypress二进制目录

修改CI配置的cache部分,把Cypress的缓存目录加入缓存路径:

cypress tests:
  stage: test
  image: cypress/browsers:node14.17.0-chrome91-ff89
  cache:
    key: package-lock.json
    paths:
      - node_modules
      - ~/.cache/Cypress  # 新增这一行,缓存Cypress二进制文件
  before_script:
    - npm install
    - npm run dev &
    - ./node_modules/.bin/wait-on http://localhost:3000
  script:
    - npm run cypress
  only:
    - merge_requests
    - master

方案2:让Cypress把二进制存在项目目录下

通过环境变量指定Cypress二进制的存储目录到项目内,这样就能被现有缓存规则覆盖:

cypress tests:
  stage: test
  image: cypress/browsers:node14.17.0-chrome91-ff89
  cache:
    key: package-lock.json
    paths:
      - node_modules
      - .cypress-cache  # 对应下面设置的存储目录
  variables:
    CYPRESS_CACHE_FOLDER: .cypress-cache  # 强制二进制存在项目目录
  before_script:
    - npm install
    - npm run dev &
    - ./node_modules/.bin/wait-on http://localhost:3000
  script:
    - npm run cypress
  only:
    - merge_requests
    - master

方案3:安装后强制验证二进制

如果不想改缓存规则,可以在npm install后加验证命令,自动补全缺失的二进制:

cypress tests:
  stage: test
  image: cypress/browsers:node14.17.0-chrome91-ff89
  cache:
    key: package-lock.json
    paths:
      - node_modules
  before_script:
    - npm install
    - npx cypress verify  # 验证二进制,缺失则自动下载
    - npm run dev &
    - ./node_modules/.bin/wait-on http://localhost:3000
  script:
    - npm run cypress
  only:
    - merge_requests
    - master
排查建议
  • 查看CI日志里npm install的输出,对比首次和后续运行时Cypress二进制的下载状态
  • 临时注释掉cache块禁用缓存,看是否还会报错,确认问题是否由缓存导致
  • 检查package.json里cypress的版本,确保和使用的cypress/browsers镜像版本兼容

内容的提问来源于stack exchange,提问作者DraxDomax

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 09:55:24