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]Error: Cannot find module '@playwright/test' Playwright Version 1.33.0 #23098

Closed
Tracked by #5119
carolinapanther1 opened this issue May 17, 2023 · 13 comments
Closed
Tracked by #5119
Assignees
Labels

Comments

@carolinapanther1
Copy link

carolinapanther1 commented May 17, 2023

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 carolinapanther1 changed the title [BUG]Error: Cannot find module '@playwright/test' [BUG]Error: Cannot find module '@playwright/test' Playwright Version 1.33.0 May 17, 2023
@dgozman
Copy link
Contributor

dgozman commented May 18, 2023

@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.

@john-cyndx
Copy link

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

@dgozman
Copy link
Contributor

dgozman commented May 20, 2023

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

@carolinapanther1
Copy link
Author

carolinapanther1 commented May 22, 2023

@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
Copy link
Author

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??

@pereorga
Copy link

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

@dz1dz1on
Copy link

dz1dz1on commented May 22, 2023

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.

@KunalOjha
Copy link

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?

@dgozman
Copy link
Contributor

dgozman commented May 22, 2023

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

@carolinapanther1
Copy link
Author

carolinapanther1 commented May 23, 2023

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.

@warorc
Copy link

warorc commented Jun 8, 2023

@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?

@jasonk
Copy link

jasonk commented Jun 9, 2023

See my comment here for a fix if you need to have both installed: #6981 (comment)

@prabhubabu
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants