How do I switch from TypeScript to JavaScript as the default language in Playwright without reinstalling it?


Switching from TypeScript to JavaScript in Playwright

No need to uninstall and reinstall Playwright to switch from TypeScript to JavaScript. Just tweak your project's configuration settings and scripts.

Modify tsconfig.json

First, head to your project's tests folder. If there's no tsconfig.json file, create one. Then, add or modify the compiler options:

  "compilerOptions": {
    "target": "ESNext",
    "module": "commonjs",
    "moduleResolution": "Node",
    "sourceMap": true,
    "outDir": "../tests-out"

These options tell TypeScript to target ESNext, use the CommonJS module system, generate source maps, and output transpiled JavaScript files into a tests-out directory.

Update package.json

Next, open your package.json file. In the "scripts" section, add or modify these scripts:

  "scripts": {
    "pretest": "<command-to-run-typescript> -p tests/tsconfig.json",
    "test": "<command-to-run-playwright> -c tests-out"

Replace <command-to-run-typescript> with a command to run TypeScript compilation on your tests, like "tsc --incremental" or "npx tsc --incremental". Replace <command-to-run-playwright> with a command to run Playwright tests, like "playwright test" or "npx playwright test".

The pretest script runs TypeScript compilation on your tests using the tsconfig.json file. The test script executes the JavaScript tests in the tests-out directory.

After saving these changes, run npm run test to build and run your tests using JavaScript instead of TypeScript.


Thank you!
Was this helpful?
Still have questions?

If you still have questions, please ask a question and I will try to answer it.

Related Discord Threads

Related Questions

AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email luc@ray.run.