[BUG] Unable to import module in test [ERR_REQUIRE_ESM]
See original GitHub issueContext:
- 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:
- Created a year ago
- Reactions:1
- Comments:7 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@dgozman your example indeed works for me! However, with
PW_EXPERIMENTAL_TS_ESM=1set I can’t import any local files so I doesn’t make much sense.ex:
import { mock } from '../../src/mock'the file
another drawback of this approach is that I can’t use
requirestatement. I hope I can replace them with dynamic imports at some point.requirestatements are still recommended in the official docs https://playwright.dev/docs/test-authglobalSetup: require.resolve('./global-setup')@dgozman i have this on windows 10.