Skip to content

Error "ighthouseLib is not a function" after upgrade lighthouse package to version 12.x #239

@mjalav

Description

@mjalav

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch @cypress-audit/lighthouse@1.4.2 for the project I'm working on.

I got an error when I upgraded lighthouse to version 12.x and the error was "lighthouseLib is not a function"
Here is the diff that solved my problem:

diff --git a/node_modules/@cypress-audit/lighthouse/src/task.js b/node_modules/@cypress-audit/lighthouse/src/task.js
index 72a6526..5553930 100644
--- a/node_modules/@cypress-audit/lighthouse/src/task.js
+++ b/node_modules/@cypress-audit/lighthouse/src/task.js
@@ -1,13 +1,8 @@
-const lighthouseLib = require("lighthouse/core/index.cjs").legacyNavigation;
-const {
-  computeCategories,
-  computeAudits,
-  compareWithThresholds,
-} = require("./helpers");
+const { computeCategories, computeAudits, compareWithThresholds } = require('./helpers');
 
 const lighthouse =
   (callback) =>
-  ({ url, thresholds, opts = {}, config }) => {
+    async ({ url, thresholds, opts = {}, config }) => {
     if (global.cypress_audit_port) {
       opts.port = global.cypress_audit_port;
 
@@ -19,10 +14,13 @@ const lighthouse =
         opts.disableStorageReset = true;
       }
 
-      return lighthouseLib(url, opts, config).then((results) => {
-        if (callback) {
-          callback(results);
-        }
+  // Use dynamic import for 'lighthouse'
+  const lighthouse = (await import('lighthouse')).default;
+
+  return lighthouse(url, opts, config).then((results) => {
+    if (callback) {
+      callback(results);
+    }
 
         const computedAudits = computeAudits(results.lhr.audits);
         const computedCategories = computeCategories(results.lhr.categories);

This issue body was partially generated by patch-package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions