Skip to content

ESM Compatibility Issue with playwright-bdd #9

@AndyN27

Description

@AndyN27

Issue Description

When using @auto-browse/auto-browse (an ESM module) with playwright-bdd (a CommonJS module), there is a module loading conflict that causes Playwright to be loaded twice in different module systems.

Environment

  • Node.js: v22.7.0
  • @auto-browse/auto-browse: ^0.1.7
  • playwright-bdd: ^8.4.1
  • @playwright/test: ^1.55.0

Steps to Reproduce

  1. Create a new project with playwright-bdd
  2. Install @auto-browse/auto-browse
  3. Create a step definition file that imports from @auto-browse/auto-browse
  4. Run npx bddgen

Error Message

Error: Requiring @playwright/test second time ... Error [ERR_REQUIRE_ESM]: require() of ES Module [...]/node_modules/@auto-browse/auto-browse/dist/index.js not supported. Instead change the require of index.js to a dynamic import() which is available in all CommonJS modules.

Attempted Solutions

  1. Using dynamic import:
let auto: any;
(async () => {
  const autoBrowse = await import("@auto-browse/auto-browse");
  auto = autoBrowse.auto;
})();
  1. Adding "type": "module" to package.json and using ES modules:
import { auto } from "@auto-browse/auto-browse";
  1. Using ts-node/register in playwright.config.ts:
export default defineConfig({
  workers: 1,
  fullyParallel: false,
  require: ['ts-node/register'],
  // ...
});

All these solutions result in the same error where Playwright is being loaded twice in different module systems.

Expected Behavior

The module should be able to be imported and used within playwright-bdd step definitions without causing module loading conflicts.

Impact

This issue prevents users from using @auto-browse/auto-browse with playwright-bdd, which are both valuable tools for test automation.

Possible Solutions

  1. Consider providing a CommonJS build of @auto-browse/auto-browse
  2. Add documentation about ESM compatibility and recommended workarounds
  3. Consider supporting dual package hazard scenarios where Playwright is loaded in both ESM and CommonJS contexts

Additional Context

This issue appears to be more prominent in Node.js v22.7.0 which has stricter module loading rules. The core issue is the incompatibility between ESM and CommonJS module systems when dealing with complex dependencies like Playwright.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions