Search code examples
node.jspluginsimportcypressrequire

How to import plugins in Cypress 9 with Node 16?



I'm struggling trying to import external libraries to the plugin file.
If I do
const clipboardy = require('clipboardy')

it says "Error [ERR_REQUIRE_ESM]: require() of ES Module /[...]/e2e/node_modules/clipboardy/index.js from /[...]/e2e/cypress/plugins/index.js not supported.". I tried also with

import clipboardy from 'clipboardy'

but this is still not working. I really don't understand how to solve.
Can you please help me? I'm on node 16.10 with cypress 9.0.0 Thanks in advance

EDIT: the plugin file

const clipboardy = require("clipboardy");
const csv = require("node-xlsx").default;
const fs = require("fs");

const { lighthouse, pa11y, prepareAudit } = require("cypress-audit");

module.exports = (on, config) => {
  on("task", {
    parseXlsx({ filePath }) {
      return new Promise((resolve, reject) => {
        try {
          const jsonData = csv.parse(fs.readFileSync(filePath));
          resolve(jsonData);
        } catch (e) {
          reject(e);
        }
      });
    },

    getClipboard() {
      return clipboardy.readSync();
    },

    lighthouse: lighthouse(lighthouseReport => {
      const categories = lighthouseReport.lhr.categories;

      const audits = lighthouseReport.lhr.audits;

      const formattedAudit = Object.keys(audits).reduce(
        (metrics, curr) => ({
          ...metrics,
          [curr]: audits[curr].numericValue
        }),
        {}
      );

      const formattedCategories = Object.keys(categories).reduce(
        (metrics, curr) => ({
          ...metrics,
          [curr]: categories[curr].score * 100
        }),
        {}
      );

      const results = {
        url: lighthouseReport.lhr.requestedUrl,
        ...formattedCategories
      };

      console.log("Lighthouse results: [");
      console.log(results);
      console.log("]");

      //      fs.writeJSONSync("../results/audit.json", results);
      //      fs.writeFileSync(
      //        "audit.json",
      //        Buffer.from(JSON.stringify(results), "utf8")
      //      );
    }),

    pa11y: pa11y(pa11yReport => {
      console.log("pa11y results: [");
      console.log(pa11yReport);
      console.log("]");
    })
  });

  on("before:browser:launch", (browser, launchOptions) => {
    prepareAudit(launchOptions);

    if (browser.name === "chromium") {
      launchOptions.args.push(
        "--disable-features=CrossSiteDocumentBlockingIfIsolating,CrossSiteDocumentBlockingAlways,IsolateOrigins,site-per-process"
      );
      launchOptions.args.push(
        "--load-extension=cypress/extensions/Ignore-X-Frame-headers_v1.1"
      );
      launchOptions.args.push("--disable-dev-shm-usage");

      return launchOptions;
    }

    return launchOptions;
  });

  return config;
};

Solution

  • The problem was related ti clipboardy, which in version 3 became a ES Only modulo, not compatibile with Cypress. Downgrading to v2.3 solved the issue