playwright: [Bug]: 1.48.0 missing chromium-bidi dependency

Version

1.48.0

Steps to reproduce

  1. Clone my repo at https://github.com/halljus/playwright-1.48.0-repro
  2. npm install
  3. npx playwright install
  4. npm run dev
  5. Browse to http://localhost:5173

Observe in the server console that the ~bundler~ compiler (esbuild) cannot resolve chromium-bidi.

Expected behavior

It should just work (either by not requiring a devDependencies library, promoting it to dependencies, or updating documentation with further instructions).

This error does not occur in playwright v1.47.2, the latest version prior to v1.48.0.

Actual behavior

✘ [ERROR] Could not resolve "chromium-bidi/lib/cjs/bidiMapper/BidiMapper"

    node_modules/playwright-core/lib/server/bidi/bidiOverCdp.js:7:49:
      7 │ var bidiMapper = _interopRequireWildcard(require("chromium-bidi/lib/cjs/bidiMapper/BidiMapper"));
        ╵                                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "chromium-bidi/lib/cjs/bidiMapper/BidiMapper" as external to exclude it from
  the bundle, which will remove this error and leave the unresolved path in the bundle. You can also
  surround this "require" call with a try/catch block to handle this failure at run-time instead of
  bundle-time.

✘ [ERROR] Could not resolve "chromium-bidi/lib/cjs/cdp/CdpConnection"

    node_modules/playwright-core/lib/server/bidi/bidiOverCdp.js:8:56:
      8 │ var bidiCdpConnection = _interopRequireWildcard(require("chromium-bidi/lib/cjs/cdp/CdpConnection"));
        ╵                                                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  You can mark the path "chromium-bidi/lib/cjs/cdp/CdpConnection" as external to exclude it from the
  bundle, which will remove this error and leave the unresolved path in the bundle. You can also
  surround this "require" call with a try/catch block to handle this failure at run-time instead of
  bundle-time.

11:01:44 AM [vite] error while updating dependencies:
Error: Build failed with 2 errors:
node_modules/playwright-core/lib/server/bidi/bidiOverCdp.js:7:49: ERROR: Could not resolve "chromium-bidi/lib/cjs/bidiMapper/BidiMapper"
node_modules/playwright-core/lib/server/bidi/bidiOverCdp.js:8:56: ERROR: Could not resolve "chromium-bidi/lib/cjs/cdp/CdpConnection"
    at failureErrorWithLog (C:\Users\user\dev\playwright-1.48.0-repro\node_modules\vite\node_modules\esbuild\lib\main.js:1472:15)
    at C:\Users\user\dev\playwright-1.48.0-repro\node_modules\vite\node_modules\esbuild\lib\main.js:945:25
    at C:\Users\user\dev\playwright-1.48.0-repro\node_modules\vite\node_modules\esbuild\lib\main.js:1353:9
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Additional context

The repro is a fresh Remix project, but it’s a Vite issue.

Relevant code:

Environment

System:
  OS: Windows 10 10.0.19045
  CPU: (12) x64 13th Gen Intel(R) Core(TM) i5-1345U
  Memory: 38.74 GB / 63.64 GB
Binaries:
  Node: 20.17.0 - ~\AppData\Local\nvs\default\node.EXE
  npm: 10.8.2 - ~\AppData\Local\nvs\default\npm.CMD
IDEs:
  VSCode: 1.94.1 - C:\Users\user\AppData\Local\Programs\Microsoft VS Code\bin\code.CMD
Languages:
  Bash: 5.1.16 - C:\WINDOWS\system32\bash.EXE
npmPackages:
  playwright: 1.48.0 => 1.48.0 

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 12
  • Comments: 21 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Why is this closed ? bug still present in 1.49.0.

Closing as per above. Not sure if it is worth bringing up with the esbuild / vite folks to let them know that they could do a better job at detecting playwright as a server-side only import.

Could you guys reopen this issue. It has not been fixed.

@halljus Im seeing this issue when using vitest, which use vite, however I can’t configure its server to exclude chromium-bidi like you did

Image

Image

I’m using vitest “browser” feature with playwright https://vitest.dev/guide/browser/#examples

This is my vitest.worspace.mjs

import { defineWorkspace } from "vitest/config";
import * as path from "path";

export default defineWorkspace([
  {
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "./src"),
        "@/nodes": path.resolve(__dirname, "./src/nodes"),
      },
    },
    test: {
      name: "unit",
      include: ["test/unit/nodes/**/*.{test,spec}.js"],
      globals: true,
      environment: "node",
      reporters: ["html"],
      coverage: {
        enabled: true,
        provider: "istanbul",
        reportsDirectory: "./coverage",
        reporter: ["html"],
      },
    },
  },
  {
    test: {
      name: "browser",
      include: ["test/browser/**/*.{test,spec}.js"],
      browser: {
        provider: "playwright",
        enabled: true,
        name: "chromium",
      },
      globalSetup: "./global-setup.mjs",
    },
  },
]);

my global-setup.mjs is just starting another application in the background, and has nothing to do with this error, so I’m not posting it here.

dev dependencies

"@vitest/browser": "^2.1.4",
"playwright": "^1.48.2",
"vitest": "^2.1.4",

Image

I also had to run npx playwright install to download browsers, which was a recommended command playwright itself showed on the terminal.

@AllanOricil Yeah, I don’t know. I still think Microsoft needs to revisit the change in 1.48.0 rather than claiming all the different build tools in the ecosystem are wrong, but what do I know? They’re smarter than I. 🙂

Marking chromium-bidi as external in ESBuild might make it work.

I tried something like that but apparently I used the wrong option. This did work, though:

// vite.config.ts

optimizeDeps: {
  exclude: ['chromium-bidi']
},

Vite’s dependency pre-bundling optimization is what was asking esbuild to resolve chromium-bidi.

This is still a problem using SST v2 reverting to 1.47.2 fixes the issue.

I don’t think playwright is ready to be bundled, what is your use case?

I might’ve misspoken by calling it a bundler, as I don’t think it’s bundling in this context.

The use case is using Playwright on the server in a Remix app ~(could be Next.js or any other modern web framework)~. It’s been working fine like this for as long as I can remember, it just stopped with 1.48.0 yesterday. 😛

There’s a comment right above the offending import:

// Chrome doesn't support Bidi, we create Bidi over CDP which is used by Chrome driver.
// bidiOverCdp depends on chromium-bidi which we only have in devDependencies, so
// we load bidiOverCdp dynamically.
const bidiTransport = await require('./bidiOverCdp').connectBidiOverCdp(transport); 

Dynamically imported or not, wouldn’t bidiOverCdp’s dependencies (chromium-bidi) need to be installed in consuming projects? Unless maybe there’s a check elsewhere in the calling code to only load bidiChromium.ts if developing locally within the Playwright project, but if there is - it doesn’t appear to be working.

Marking chromium-bidi as external in ESBuild might make it work.

Ran into this when using bun build in a docker based setup.

For future searchers, since nobody gave an explicit bun fix yet, here is one way to get around it with bun: bun build **your build settings** --external electron --external chromium-bidi",

Edit: see https://github.com/microsoft/playwright/issues/35479#issuecomment-2777607491

Use the below at your own risk. We no longer bundle Playwright in our projects.

Stale info

For what it’s worth, our team has been bundling playwright for some time now. We use express + playwright to run a pdf-service (you give it an html string, it gives you a pdf).

Here are some files for reference if it helps.

  • We use yarn as package manager, so adapt to your needs
  • We use esbuild to bundle to cjs (omit the excludeVendorFromSourceMapPlugin part if you don’t care about that)
  • Plus the change of chromium-bidi marked as external as mentioned above

esbuild.config.mjs:

import fs from 'node:fs';
import * as esbuild from 'esbuild';

// Only create source maps for our code. See https://github.com/nodejs/node/issues/41541#issuecomment-1457485140
const excludeVendorFromSourceMapPlugin = {
  name: 'excludeVendorFromSourceMapPlugin',
  setup(build) {
    const emptySourceMapAsBase64 = Buffer.from(JSON.stringify({ version: 3, sources: [''], mappings: 'A' })).toString(
      'base64'
    );
    build.onLoad({ filter: /node_modules.+\.(js|ts)$/ }, (args) => {
      return {
        contents: `${fs.readFileSync(args.path, 'utf8')}\n//# sourceMappingURL=data:application/json;base64,${emptySourceMapAsBase64}`,
        loader: 'default'
      };
    });
  }
};

console.log('Bundling code');

await esbuild.build({
  logLevel: 'info',
  plugins: [excludeVendorFromSourceMapPlugin],
  external: [
    './chromium/appIcon.png',
    './loader',
    // see https://github.com/microsoft/playwright/issues/33031#issuecomment-2404366111
    'chromium-bidi'
  ],
  entryPoints: ['./src/index.ts'],
  bundle: true,
  sourcemap: 'linked',
  sourcesContent: false, // omit the original source code. We only need sensible stack traces. See https://esbuild.github.io/api/#sources-content
  platform: 'node',
  format: 'cjs', // must correspond to the type field in package.json
  outfile: './dist/index.cjs'
});

Dockerfile

# For all available image tags, see:
# https://mcr.microsoft.com/en-us/product/playwright/tags
ARG PLAYWRIGHT_IMAGE=mcr.microsoft.com/playwright:v1.50.0-noble

# hadolint ignore=DL3006
FROM ${PLAYWRIGHT_IMAGE} AS builder

ARG NODE_ENV=production

WORKDIR /home/pwuser/app

# Enable yarn
RUN corepack enable

COPY . .

# Install packages, do production build, run unit tests
RUN yarn install \
  && yarn build \
  && yarn test

# hadolint ignore=DL3006
FROM ${PLAYWRIGHT_IMAGE} AS final

WORKDIR /home/pwuser/app

ENV NODE_ENV=production
ENV TZ=America/New_York

# Tini is a container init system
# See https://github.com/krallin/tini
RUN apt-get update && \
  apt-get -y --no-install-recommends install tini=0.* && \
  rm -rf /var/lib/apt/lists/* && \
  apt-get clean

USER pwuser

# Only copy needed files
COPY --from=builder --chown=pwuser:pwuser /home/pwuser/app/dist /home/pwuser/app/dist

EXPOSE 4006

ENTRYPOINT [ "tini", "--" ]

CMD [ "node", "--enable-source-maps", "/home/pwuser/app/dist/index.cjs" ]

Resolved this in Nuxt 3 config with:

nitro: {
  rollupConfig: {
    external: ['playwright-core'],
  },

So i’m seeing this same issue when trying to build a Nuxt3 (3.13.2) app with ssr: true. I set the optimizeDeps, as suggested in the comment above but it still errors.

I mitigated this by downgrading and pinning the exact version 1.47.2 in my package.json "@playwright/test": "1.47.2"

However, what really confuses me is that this error comes up when i run the build nuxt build. Why is playwright involved in that at all? this is bundling my frontend code as a universal app so why the test framework?

The only place playwright is called inside my code base is the test files (excluded and added to .nuxtignore just in case). and in the npm test script. So… why is it attempting to bundle playwright?

bun build also has the same error with 1.48 but not 1.47. I’m pre-compiling a typescript project to run in a container. I think this is normal?

7 | var bidiMapper = _interopRequireWildcard(require("chromium-bidi/lib/cjs/bidiMapper/BidiMapper"));
                                                     ^
error: Could not resolve: "chromium-bidi/lib/cjs/bidiMapper/BidiMapper". Maybe you need to "bun install"?
    at /home/mark/code/forecast/node_modules/playwright-core/lib/server/bidi/bidiOverCdp.js:7:50

I don’t think playwright is ready to be bundled, what is your use case?