question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] Unable to import module in test [ERR_REQUIRE_ESM]

See original GitHub issue

Context:

  • Playwright Version: 1.20.1 (also tried beta)
  • Node.js version: 16

Code Snippet

import { test } from '@playwright/test'
import getport from 'get-port'

test.describe('suite', () => {
    test('test', async ({ page }) => {
        console.log(await getport())
        await page.goto('https://playwright.dev')
    })
})

Describe the bug

Playwright fails with no test found error due to import import getport from 'get-port' of a module.

PW_EXPERIMENTAL_TS_ESM=1 doesn’t help anyhow in any combination with or without "type": "module" in the package.json

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
grybykmcommented, Mar 28, 2022

@dgozman your example indeed works for me! However, with PW_EXPERIMENTAL_TS_ESM=1 set I can’t import any local files so I doesn’t make much sense.

ex: import { mock } from '../../src/mock'

the file

import { Page } from '@playwright/test'

export const mock = async (page: Page) => {}

another drawback of this approach is that I can’t use require statement. I hope I can replace them with dynamic imports at some point.

require statements are still recommended in the official docs https://playwright.dev/docs/test-auth globalSetup: require.resolve('./global-setup')

1reaction
frederikhorscommented, Mar 26, 2022

@dgozman i have this on windows 10.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Import ES module in Next.js ERR_REQUIRE_ESM
I think the problem is that Webpack (or Babel) is transforming all import s to require() s but ky is a pure ES...
Read more >
Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
js to a dynamic import() which is available in all CommonJS modules" occurs because a package you are importing has been converted to...
Read more >
Error ERR REQUIRE ESM | Must use import to load ES Module
Your browser can't play this video. Learn more. Switch camera.
Read more >
Resolve "Unable to import module" errors from Python ...
This is because Lambda isn't prepackaged with all Python libraries. To resolve this error, create a deployment package or Lambda layer that ...
Read more >
Trying to set up next.js test - slicemachine error ...
Trying to set up next.js test - slicemachine error ERR_REQUIRE_ESM ... Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found