playwright: Error: playwright.config.ts: Cannot import a typescript file from an esmodule
Decided to create a new issue after seeing the one referenced below
having "type": "module" in package.json causes this error. And it seems it’s still supposed to work even with that in there.
The ‘fix’ below does indeed work.
I am now receiving this error after upgrading from 1.17.2 to 1.19.2
If I alter file node_modules/@playwright/test/lib/loader.js
isModule = false; // <--- force this to false
if (isModule) {
return await esmImport();
}
then it starts working again.
Also using the generated playwright.config.ts file does not resolve this issue.
_Originally posted by @asdf23 in https://github.com/microsoft/playwright/issues/11755#issuecomment-1048380805_
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 16
- Comments: 15 (4 by maintainers)
Same issue. The manual “fix” you proposed “worked”. I just started out experimenting with playwright in my existing SvelteKit project - with this issue causing a not so great experience tbh.
EDIT: For my GitHub Action I used
run: sed -i 's/if (isModule)/if (false)/g' node_modules/@playwright/test/lib/loader.jsto replace that variable (as @jordevorstenbosch proposed) after installing dependencies.I was trying Playwright with Vite and TypeScript. I used the vanilla-ts project sample and followed the intro docs for Playwright but when I tried to run the example test with:
I received this error:
Turns out having
"type": "module"in mypackage.jsonwas causing the issue. This was part of the default Vite setup but it seems you can swap this with"type": "commonjs"and then Playwright will run, as well as Vite building and hot-reloading fine.Sorry that still doesn’t work with version 1.25, I will make a reproduction.
@IanVS you probably figured it out:
I can’t seem to import custom
.tsfile with"type": "module"and the envPW_EXPERIMENTAL_TS_ESM=1Importing this in the playwright starter
example.spec.tsresults inCannot find moduleerror, (while it works otherwise)I’m just trying out playwright for the first time, and ran through the
npm init playwright@latestflow. I hit the error referenced in this issue (I use vite, andtype="module"), and so usedPW_EXPERIMENTAL_TS_ESM=1 npx playwright test, but now I see:I’m using node v16.13.1 and playwright 1.20.1.
Oh I see, it’s a type. I changed it to
import { test, expect, type Page } from '@playwright/test';and I’m back in business.Maybe it would be best to break that out to its own line, to work with typescript configs like mine which set
"importsNotUsedAsValues": "error",@jordevorstenbosch
PW_EXPERIMENTAL_TS_ESM=1 npm run testran for me without errors.Note that starting with 1.21 you won’t need that environment variable
I’ve made one for you here:
https://github.com/jordevorstenbosch/sk-pw-issue