abhinaba-ghosh / playwright-lighthouse

:performing_arts:: Playwright Lighthouse Audit
https://www.npmjs.com/package/playwright-lighthouse
MIT License
240 stars 28 forks source link

Getting import error when using playAudit #50

Open digracesion opened 1 year ago

digracesion commented 1 year ago

Everytime I run my tests, I'm encountering the following error message:

SyntaxError: Unexpected token 'export'

      1 | import { test, chromium } from '@playwright/test';
    > 2 | import { playAudit } from 'playwright-lighthouse';
        | ^
      3 | import lighthouse from 'lighthouse';

      at Runtime.createScriptFromCode (node_modules/jest-runtime/build/index.js:1728:14)
      at Object.<anonymous> (resources/js/tests/e2e/audit.spec.ts:2:1)

But package.json shows playwright-lighthouse": "^3.1.0, so I'm quite confused as to what's wrong here.

I tried installing npm i -D tsconfig-paths thinking that maybe this was an issue with the reading from the ts file and I'm currently seeing the following:

Error: require() of ES Module /Users/gray/yaraku/zen/node_modules/playwright-lighthouse/index.js from /Users/gray/yaraku/zen/resources/js/tests/e2e/audit.spec.ts not supported.
Instead change the require of index.js in /Users/gray/yaraku/zen/resources/js/tests/e2e/audit.spec.ts to a dynamic import() which is available in all CommonJS modules.
ahernd2 commented 1 year ago

Seeing same issue

edumserrano commented 9 months ago

I'm also getting this, seems like a duplicate of #45.

As suggested on the stackoverflow link provided on #45, adding:

"type": "module",

to my package.json solved the issue.

There's also this question at #46 but there's no response so I'm not sure if there's an alternative to the solution above.

abhishekjha7798 commented 6 months ago

What if someone does not want to add "type": "module" in their package.json because project is configured with commonjs? Do we have any other solution?

alphonse92 commented 1 month ago

What if someone does not want to add "type": "module" in their package.json because project is configured with commonjs? Do we have any other solution?

I posted my fix here without placing the "type" module

https://github.com/abhinaba-ghosh/playwright-lighthouse/issues/72