Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Unknown file extension ".ts" on fresh init when adding "type": "module" #18282

Closed
pleek91 opened this issue Oct 24, 2022 · 9 comments
Closed

Comments

@pleek91
Copy link

pleek91 commented Oct 24, 2022

Context:

  • Playwright Version: 1.27.1
  • Operating System: Mac
  • Node.js version: 16.5.0
  • Browser: N/A
  • Extra: N/A

System:

  • OS: macOS 12.6
  • Memory: 89.59 MB / 16.00 GB

Binaries:

  • Node: 16.5.0 - ~/.nvm/versions/node/v16.5.0/bin/node
  • npm: 8.3.0 - ~/.nvm/versions/node/v16.5.0/bin/npm

Languages:

  • Bash: 3.2.57 - /bin/bash

Code Snippet

N/A

Describe the bug

In a completely empty directory I ran npm init playwright@latest, and chose

✔ Do you want to use TypeScript or JavaScript? · TypeScript
✔ Where to put your end-to-end tests? · tests
✔ Add a GitHub Actions workflow? (y/N) · false
✔ Install Playwright browsers (can be done manually via 'npx playwright install')? (Y/n) · false

Then I ran npx playwright test and everything ran correctly.
Then I added "type": "module" to my package.json and ran npx playwright test again.

Received this error

TypeError: Unknown file extension ".ts" for /Users/my-name/Development/playwright-ts-bug/playwright.config.ts
@pleek91 pleek91 changed the title [BUG] Unknown file extension ".ts" on fresh init [BUG] Unknown file extension ".ts" on fresh init when adding "type": "module" Oct 24, 2022
@mxschmitt
Copy link
Member

Works for me - I can't repro! Can you make sure that you use Node.js 16+? I saw you wrote that you are using a recent one, but type: module support is only supported in Playwright in 16+.

@pleek91
Copy link
Author

pleek91 commented Oct 26, 2022

My node version is 16.5.0 so that should be fine right?

@mxschmitt
Copy link
Member

It actually needs to be a more recent one, like v16.18.0.

@pleek91
Copy link
Author

pleek91 commented Oct 27, 2022

Okay, I'll update node and try that again when I have a chance. Thank you! I'll close this issue.

@pleek91 pleek91 closed this as completed Oct 27, 2022
@acdcjunior
Copy link

I got this error in a monorepo where the package with TS tests had "type": "module", and playwright was installed in the root.

Setting "type": "module", in the root as well fixed the problem.

I don't have non "type": "module", packages, so take it with a grain of salt.

@diegohaz
Copy link

I'm also seeing this error on a monorepo project. The packages with the test files are marked with "type": "module". But, unfortunately, I can't set "type": "module" to the root package, where the playwright.config.ts file is.

It would be nice if Playwright implemented a flag or something to force it to run as ESM even without "type": "module" or maybe detect it automatically by reading a playwright.config.mts file (notice the .mts extension).

The workaround I found was running playwright with an ESM loader (make sure to install esm-loader-typescript):

NODE_OPTIONS=--loader=esm-loader-typescript playwright test

jasonkylefrank added a commit to jasonkylefrank/jasonkylefrank.com that referenced this issue May 19, 2023
In order to properly run Playwright tests in this monorepo, I had to
set "type": "module" in the root `packge.json`.  Without that I was getting
an error saying that the ".ts" was an unknown file extension.
This answer tipped-me off on this solution:
microsoft/playwright#18282 (comment)

For these initial Playwright tests, I created a temporary dummy page
and added a ".e2e.ts" file that is associated with that file.
@simonwep
Copy link

Any update on this one? This is still an issue... and I'm not sure what is the recommended workaround for that.

@M-51
Copy link

M-51 commented Jun 29, 2023

If you still have this problem, try to add playwright.config.ts file (if you don't have already) to directory where package.json of your project is.
I have no idea why it works this way.

I had simple project, I didn't need complex configuration, so I skipped config file (as far as I know it is not required), but was getting TypeError: Unknown file extension ".ts" error.

Then added empty playwright.config.ts - and it works...

If you have monorepo, every project needs to have own config file.
One playwright.config.ts in root of monorepo is not enough.

@morganney
Copy link

morganney commented Jan 13, 2024

Why is this closed? At least update the documentation for install to include that a monorepo will require use of --config when running playwright test in case your config is colocated with the package containing tests.

codewithcheese added a commit to codewithcheese/performer that referenced this issue Feb 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants