playwright icon indicating copy to clipboard operation
playwright copied to clipboard

[BUG]Error: Cannot find module '@playwright/test' Playwright Version 1.33.0

Open carolinapanther1 opened this issue 2 years ago • 8 comments
trafficstars

System info

  • Playwright Version: Version 1.33.0
  • Operating System: Windows 11
  • Browser: [All, Chromium, Firefox, WebKit]
  • Other info:
  • Npm -v : 9.6.6
  • Node -v: v18.16.0
  • Editor: Visual studio code 1.78.2

Description: I have been having issues with playwright and javascript for days, I have been deleting node js, visual studio code with all the extentions, after reinstalling it's the same error, I have the environment variables for node js also all set, and still, run constantly npm install, but still get the following error.

Test steps:

  1. Open visual studio code editor and open terminal on root of folder
  2. Run script: npm init playwright@latest
  3. After that I note that the node_modules is never installed plaaaaywriight1
  4. It will give me an error Cannot find module '@playwright/test' or its corresponding type declarations.ts(2307), so I always have to delete @//ts-check because it always gets installed even tho I select javascript, that comment gets also added on the playwright.config.js , /tests and /tests-examples, so I have to remove it
  5. After that I try to run on the root of the proyect: npx playwright test example.spec.js
  6. It will give me an error so I have to run: npm i -D @playwright/test
  7. After that I run npx playwright test example.spec.js and I get this error.

The Error is the following one:

Error: Cannot find module '@playwright/test' Require stack:

  • C:\Users\andre\OneDrive\Documentos\PlaywrightTest\playwright.config.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\transform.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\testType.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\test.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\reporters\multiplexer.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\runner\reporters.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\runner\runner.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\cli.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\node_modules\playwright-core\lib\cli\cli.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\node_modules\playwright-core\cli.js
  • C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\cli.js at Function.Module._resolveFilename (node:internal/modules/cjs/loader:1075:15) at Function.resolveFilename (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\transform.js:173:36) at Function.Module._load (node:internal/modules/cjs/loader:920:27) at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at Object. (C:\Users\andre\OneDrive\Documentos\PlaywrightTest\playwright.config.js:1:35)
    at Module._compile (node:internal/modules/cjs/loader:1254:14) at Module.f._compile (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\utilsBundleImpl.js:16:994) at Module._extensions..js (node:internal/modules/cjs/loader:1308:10) at Object.i..ot._extensions. (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\utilsBundleImpl.js:16:1010) at Module.load (node:internal/modules/cjs/loader:1117:32) at Function.Module._load (node:internal/modules/cjs/loader:958:12) at Module.require (node:internal/modules/cjs/loader:1141:19) at require (node:internal/modules/cjs/helpers:110:18) at requireOrImport (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\transform.js:154:20) at requireOrImportDefaultObject (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\configLoader.js:78:53) at ConfigLoader.loadConfigFile (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\common\configLoader.js:54:26) at runTests (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\cli.js:126:55)
    at ji. (C:\Users\andre\AppData\Roaming\npm\node_modules@playwright\test\lib\cli.js:48:7) {
    code: 'MODULE_NOT_FOUND', requireStack: [ 'C:\Users\andre\OneDrive\Documentos\PlaywrightTest\playwright.config.js', 'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\common\transform.js',
    'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\common\testType.js',
    'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\common\test.js',
    'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\reporters\multiplexer.js', 'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\runner\reporters.js',
    'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\runner\runner.js',
    'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\lib\cli.js', 'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\node_modules\playwright-core\lib\cli\cli.js', 'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\node_modules\playwright-core\cli.js', 'C:\Users\andre\AppData\Roaming\npm\node_modules\@playwright\test\cli.js' ] }

// playwright.config.js

**const { defineConfig, devices } = require('@playwright/test');

/**
 * Read environment variables from file.
 * https://github.com/motdotla/dotenv
 */
// require('dotenv').config();

/**
 * @see https://playwright.dev/docs/test-configuration
 */
module.exports = defineConfig({
  testDir: './tests',
  /* Run tests in files in parallel */
  fullyParallel: true,
  /* Fail the build on CI if you accidentally left test.only in the source code. */
  forbidOnly: !!process.env.CI,
  /* Retry on CI only */
  retries: process.env.CI ? 2 : 0,
  /* Opt out of parallel tests on CI. */
  workers: process.env.CI ? 1 : undefined,
  /* Reporter to use. See https://playwright.dev/docs/test-reporters */
  reporter: 'html',
  /* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
  use: {
    /* Base URL to use in actions like `await page.goto('/')`. */
    // baseURL: 'http://127.0.0.1:3000',

    /* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
    trace: 'on-first-retry',
  },

  /* Configure projects for major browsers */
  projects: [
    {
      name: 'chromium',
      use: { ...devices['Desktop Chrome'] },
    },

    {
      name: 'firefox',
      use: { ...devices['Desktop Firefox'] },
    },

    {
      name: 'webkit',
      use: { ...devices['Desktop Safari'] },
    },

    /* Test against mobile viewports. */
    // {
    //   name: 'Mobile Chrome',
    //   use: { ...devices['Pixel 5'] },
    // },
    // {
    //   name: 'Mobile Safari',
    //   use: { ...devices['iPhone 12'] },
    // },

    /* Test against branded browsers. */
    // {
    //   name: 'Microsoft Edge',
    //   use: { ...devices['Desktop Edge'], channel: 'msedge' },
    // },
    // {
    //   name: 'Google Chrome',
    //   use: { ..devices['Desktop Chrome'], channel: 'chrome' },
    // },
  ],

  /* Run your local dev server before starting the tests */
  // webServer: {
  //   command: 'npm run start',
  //   url: 'http://127.0.0.1:3000',
  //   reuseExistingServer: !process.env.CI,
  // },
});**

package.json

{ "name": "playwrighttest", "version": "1.0.0", "description": "", "main": "index.js", "scripts": {}, "keywords": [], "author": "", "license": "ISC" }

I have tryed a lot of different ways but the issue is that I can't proceed and will have that error I showed no matter how many I try to reproduce different steps to approach to this issue, if anyone has a suggestion or solution I'll be happy to hear it

carolinapanther1 avatar May 17 '23 19:05 carolinapanther1

@carolinapanther1 It looks like your have @playwright/test installed globally in C:\Users\andre\AppData\Roaming\npm and it is being picked up. I'd recommend removing the contents of C:\Users\username\AppData\Roaming\npm and trying to call npm init playwright@latest in a new directory.

dgozman avatar May 18 '23 16:05 dgozman

I'm having this exact issue upon installing version 1.34.0 of playwright. I've tried this solution and it either gives me a response similar to the above or this response

Please install @playwright/test package to use Playwright Test. npm install -D @playwright/test

I've tried to follow the steps listed above. My tests were running normally before upgrading to version 1.34.0 of playwright

john-cyndx avatar May 20 '23 11:05 john-cyndx

@john-cyndx You probably have "playwright" package installed as well? If so, run npm uninstall playwright, because you don't need both.

dgozman avatar May 20 '23 14:05 dgozman

@carolinapanther1 It looks like your have @playwright/test installed globally in C:\Users\andre\AppData\Roaming\npm and it is being picked up. I'd recommend removing the contents of C:\Users\username\AppData\Roaming\npm and trying to call npm init playwright@latest in a new directory.

I still have the same issue , I am still stuck, I have deleted node js, reinstalled playwright in various ways, to be honest the playwright dev install steps documentation is not very clear to me, should I just try to install a older version to maybe solve this issue?

carolinapanther1 avatar May 22 '23 00:05 carolinapanther1

I'm having this exact issue upon installing version 1.34.0 of playwright. I've tried this solution and it either gives me a response similar to the above or this response

Please install @playwright/test package to use Playwright Test. npm install -D @playwright/test

I've tried to follow the steps listed above. My tests were running normally before upgrading to version 1.34.0 of playwright

Could you find a solution for this issue??

carolinapanther1 avatar May 22 '23 00:05 carolinapanther1

Same problem here, using yarn, in a project having both @playwright/test and playwright, since upgrading to 1.34.0.

pereorga avatar May 22 '23 08:05 pereorga

Idk why but when I've added npx to my command it started to work. Without it, I had similar error like you guys. This did not happened on previous version (1.33).

EDIT: Ok I've needed to remove playwright from devDependencies on my side.

dz1dz1on avatar May 22 '23 10:05 dz1dz1on

Removing the "playwright" package does in fact resolve this for me in v1.34, the problem I have is I need to use the chromium module from "playwright" const { chromium } = require('playwright'); so I can't simply remove it. Is this module importable in @playwright/test?

KunalOjha avatar May 22 '23 21:05 KunalOjha

@KunalOjha Yes, const { chromium } = require('@playwright/test') works equally well.

dgozman avatar May 22 '23 22:05 dgozman

I resolved this issue by taking the following steps:

  1. I deleted all the node_modules directories, including those from different projects.
  2. I uninstalled Node.js and removed it from my system completely.
  3. I also removed Chocolatey, which was installed on my system.
  4. Upon further investigation, I discovered that I had two user profiles (C:\Users) on my system. Each time I ran node_modules, it would search between both users, causing confusion and conflicts.
  5. To address this, I accessed the environment variables on my system.
  6. I removed the paths associated with npm and Node.js, ensuring that no remnants were left.(I believe this issue may have been caused by having multiple paths in the environment variable settings, leading to conflicting directories.)
  7. I reinstalled Node.js very carefully assuring the environment variables are on the same Directory even with chocolatery
  8. I opened windows powershell as an admin
  9. Created a proyect being very careful to add the proyect on the same user profile I have node js on the environment variable
  10. Ran npm init playwright@latest and issue solved

I am truly grateful for the assistance provided by everyone throughout this debugging process Please let me know if there's anything else I can help with or if you have any further questions.

carolinapanther1 avatar May 23 '23 01:05 carolinapanther1

@john-cyndx You probably have "playwright" package installed as well? If so, run npm uninstall playwright, because you don't need both.

Why I don't need both? We have two projects in monorepo, one is using playwright for their own purposes, and I as QA use @playwright/test for testing?

warorc avatar Jun 08 '23 06:06 warorc

See my comment here for a fix if you need to have both installed: https://github.com/Microsoft/playwright/issues/6981#issuecomment-1584573476

jasonk avatar Jun 09 '23 13:06 jasonk

Hello All, I have experienced the same issue and fixed using below solutions.

Got below error while trying to run playwright scripts in docker :

playwright_docker  | Error: Cannot find module '@playwright/test'
playwright_docker  | Require stack:
playwright_docker  | - /app/playwright.config.ts

Solution :

Step 1: Remove all the existing docker images which are related to the current project/playwright using the command "docker rmi IMAGE NAME or IMAGE ID" / "docker rmi -f IMAGE NAME or IMAGE ID"

Step 2: Update from "WORKDIR /app" to "WORKDIR /root/app" in Dockerfile - This may be optional

Step 3: Make sure "headless: true" in the playwright.config.ts

Hope this helps.

prabhubabu avatar Mar 20 '24 04:03 prabhubabu

Sorry but is this going to be fixed?

I set up a new repo with npm install playwright, I want to use #page: Page;, cannot, because private identifies are available above ECMA2015, have to set up tsconfig.json with "target": "ES2018" or 2023 or whatever and that makes import { Page, Locator, test } from '@playwright/test' go red under @playwright/test: "cannot find module" yada yada yada, can it be just fixed without need of multiple threads across multiple forums of how to deal with this? Can it just work please?

I have playwright/test in devDependencies because playwright in dependencies causes the same

sizyrg avatar Jul 16 '24 12:07 sizyrg