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

Bitbucket Pipelines中Playwright提示Cannot find module问题求助

解决Bitbucket Pipeline中Playwright的@playwright/test模块找不到问题

使用版本

Playwright版本: 1.49.0

重现场景

涉及文件

bitbucket-pipelines.yml

image: mcr.microsoft.com/playwright:v1.49.0-noble

pipelines:
  default:
    - step:
        name: Install dependencies
        caches:
          - node
        script:
          - npm ci
          - npx playwright install-deps
          - npx playwright install
    - step:
        name: Run Playwright tests
        script:
          - npx playwright test

package.json

{
  "name": "auxo-ael-fork-sakib",
  "version": "1.0.0",
  "description": "Test Automation framework using playwright and typescript",
  "main": "index.js",
  "scripts": {
    "lint": "eslint .",
    "format": "prettier --write .",
    "generate:report": "allure generate ./test-reports/allure-result --clean -o test-reports/allure-report",
    "open:report": "allure open ./test-reports/allure-report",
    "test:stg:chrome": "cross-env TEST_ENV=staging BROWSER=DesktopChrome npx playwright test --workers=4",
    "test:stg:firefox": "cross-env TEST_ENV=staging BROWSER=DesktopFirefox npx playwright test",
    "test:qa:chrome": "cross-env TEST_ENV=qa BROWSER=DesktopChrome npx playwright test",
    "test:qa:firefox": "cross-env TEST_ENV=qa BROWSER=DesktopFirefox npx playwright test",
    "test:stg:chrome:report": "cross-env TEST_ENV=staging BROWSER=DesktopChrome npx playwright test && npm run generate:report"
  },
  "repository": {
    "type": "git",
    "url": "git+https://sakiburrahman1@bitbucket.org/auxo_solutions/auxo-ael-fork-sakib.git"
  },
  "author": "Md Sakibur Rahman (Espak)",
  "license": "ISC",
  "bugs": {
    "url": "https://sakiburrahman1@bitbucket.org/auxo_solutions/auxo-ael-fork-sakib/issues"
  },
  "homepage": "https://sakiburrahman1@bitbucket.org/auxo_solutions/auxo-ael-fork-sakib#readme",
  "devDependencies": {
    "@playwright/test": "^1.49.0",
    "@types/node": "^22.7.1",
    "allure-playwright": "^3.0.5",
    "cross-env": "^7.0.3",
    "eslint": "^9.11.1",
    "eslint-config-prettier": "^9.1.0",
    "eslint-plugin-playwright": "^1.6.2",
    "eslint-plugin-prettier": "^5.2.1",
    "prettier": "^3.3.3",
    "typescript": "^5.6.2"
  },
  "dependencies": {
    "dotenv": "^16.4.5"
  }
}

错误信息

运行流水线时出现以下错误:

npm warn exec The following package was not found and will be installed: playwright@1.49.0
Error: Cannot find module '@playwright/test'
Require stack:
- /opt/atlassian/pipelines/agent/build/playwright.config.ts
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/transform/transform.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/common/esmLoaderHost.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/runner/processHost.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/runner/loaderHost.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/runner/loadUtils.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/runner/reporters.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/runner/runner.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/lib/program.js
- /root/.npm/_npx/e41f203b7505f1fb/node_modules/playwright/cli.js
    at Function.<anonymous> (node:internal/modules/cjs/loader:1249:15)
    ...(省略重复调用栈)
}

问题原因

Bitbucket Pipeline的每个步骤都是独立的Docker容器,第一个步骤安装的依赖(包括@playwright/test)不会自动共享到第二个步骤。第二个步骤运行npx playwright test时,找不到本地项目的依赖包,只能临时下载全局的Playwright,但全局Playwright无法识别项目中安装的@playwright/test模块,从而触发MODULE_NOT_FOUND错误。

解决方案

方案1:合并安装与测试步骤(推荐)

将依赖安装和测试运行放在同一个步骤中,确保依赖在同一容器内可用:

image: mcr.microsoft.com/playwright:v1.49.0-noble

pipelines:
  default:
    - step:
        name: Install dependencies and run Playwright tests
        caches:
          - node
        script:
          - npm ci
          - npx playwright install-deps
          - npx playwright install
          - npx playwright test

方案2:使用Artifacts共享依赖

通过Bitbucket的Artifacts功能,将第一个步骤的依赖包传递到第二个步骤:

image: mcr.microsoft.com/playwright:v1.49.0-noble

pipelines:
  default:
    - step:
        name: Install dependencies
        caches:
          - node
        script:
          - npm ci
          - npx playwright install-deps
          - npx playwright install
        artifacts:
          - node_modules/**
          - .cache/**
    - step:
        name: Run Playwright tests
        script:
          - npx playwright test

方案3:在测试步骤重新安装依赖

在第二个步骤中重新执行依赖安装(会增加流水线耗时,不推荐):

image: mcr.microsoft.com/playwright:v1.49.0-noble

pipelines:
  default:
    - step:
        name: Install dependencies
        caches:
          - node
        script:
          - npm ci
          - npx playwright install-deps
          - npx playwright install
    - step:
        name: Run Playwright tests
        caches:
          - node
        script:
          - npm ci
          - npx playwright test

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 04:07:02