Skip to content

[BUG] dynamic import of ESM results in ERR_REQUIRE_ESM #17075

@Janpot

Description

@Janpot

Context:

  • Playwright Version: 1.25.1
  • Operating System: MacOs
  • Node.js version: v16.17.0

Code Snippet

import { test } from "@playwright/test";

test("dynamic import of ESM", async ({}) => {
  const { default: getPort } = await import("get-port");
  console.log(await getPort());
});

Describe the bug

From the node.js docs

Dynamic import() is supported in both CommonJS and ES modules. In CommonJS modules it can be used to load ES modules.

Yet, trying to load an ESM only package like get-port with a dynamic import statement fails with the error:

Error [ERR_REQUIRE_ESM]: require() of ES Module .../node_modules/get-port/index.js from .../tests/example.spec.ts not supported.
    Instead change the require of index.js in .../tests/example.spec.ts to a dynamic import() which is available in all CommonJS modules.

It's complaining I'm trying to use require to load an ESM only module, but as can be seen in the snippet, it's an import().

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions