Skip to content

[Bug]: Example spec uses CommonJS instead of ESM, even though type:module is set in package.json (JavaScript project) #33238

@aral

Description

@aral

Version

1.48.1

Steps to reproduce

  1. Clone my repo at https://codeberg.org/kitten/app
  2. Run npm init playwright@latest
  3. Run npx playwright test
  4. Select JavaScript for project type and enter tests/e2e as the test folder.

Expected behavior

The automatically created example spec should run without errors.

Actual behavior

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '/Users/aral/Projects/kitten/app/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///Users/aral/Projects/kitten/app/playwright.config.js:2:35
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at importModuleDynamicallyWrapper (node:internal/vm/module:431:15)
    at requireOrImport (/Users/aral/Projects/kitten/app/node_modules/playwright/lib/transform/transform.js:230:24)
    at loadUserConfig (/Users/aral/Projects/kitten/app/node_modules/playwright/lib/common/configLoader.js:94:46)
    at loadConfig (/Users/aral/Projects/kitten/app/node_modules/playwright/lib/common/configLoader.js:105:22)
    at loadConfigFromFileRestartIfNeeded (/Users/aral/Projects/kitten/app/node_modules/playwright/lib/common/configLoader.js:273:10)
    at runTests (/Users/aral/Projects/kitten/app/node_modules/playwright/lib/program.js:199:18)
    at t.<anonymous> (/Users/aral/Projects/kitten/app/node_modules/playwright/lib/program.js:54:7)

Additional context

The test being created uses CommonJS instead of ESM, even though type:module is set in package.json.

Fix

Convert example.spec.js and playwright.config.js to use ESM.

example.spec.js

// @ts-check
import { test, expect } from '@playwright/test';

test('has title', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Expect a title "to contain" a substring.
  await expect(page).toHaveTitle(/Playwright/);
});

test('get started link', async ({ page }) => {
  await page.goto('https://playwright.dev/');

  // Click the get started link.
  await page.getByRole('link', { name: 'Get started' }).click();

  // Expects page to have a heading with the name of Installation.
  await expect(page.getByRole('heading', { name: 'Installation' })).toBeVisible();
});

playwright.config.js

// @ts-check
import { defineConfig, devices } from '@playwright/test';

/**
 * Read environment variables from file.
 * https://github.com/motdotla/dotenv
 */
// require('dotenv').config({ path: path.resolve(__dirname, '.env') });

/**
 * @see https://playwright.dev/docs/test-configuration
 */
export default defineConfig({
  testDir: './tests/e2e',
  /* Run tests in files in parallel */
  fullyParallel: true,
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 1 : undefined,
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: 'html',
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {
    /* Base URL to use in actions like `await page.goto('/')`. */
    // baseURL: 'http://127.0.0.1:3000',

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry',
  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },

    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },

    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },

    /* Test against mobile viewports. */
    // {
    //   name: 'Mobile Chrome',
    //   use: { ...devices['Pixel 5'] },
    // },
    // {
    //   name: 'Mobile Safari',
    //   use: { ...devices['iPhone 12'] },
    // },

    /* Test against branded browsers. */
    // {
    //   name: 'Microsoft Edge',
    //   use: { ...devices['Desktop Edge'], channel: 'msedge' },
    // },
    // {
    //   name: 'Google Chrome',
    //   use: { ...devices['Desktop Chrome'], channel: 'chrome' },
    // },
  ],

  /* Run your local dev server before starting the tests */
  // webServer: {
  //   command: 'npm run start',
  //   url: 'http://127.0.0.1:3000',
  //   reuseExistingServer: !process.env.CI,
  // },
});

Environment

System:
OS: macOS 14.6.1
CPU: (8) x64 Intel(R) Core(TM) i5-8279U CPU @ 2.40GHz
Memory: 368.51 MB / 16.00 GB
Binaries:
Node: 20.11.1 - ~/.local/share/nvm/v20.11.1/bin/node
npm: 10.2.4 - ~/.local/share/nvm/v20.11.1/bin/npm
Languages:
Bash: 5.2.37 - /usr/local/bin/bash
npmPackages:
@playwright/test: ^1.48.1 => 1.48.1

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