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

ESM issues #835

Open
pcanella opened this issue May 28, 2024 · 2 comments
Open

ESM issues #835

pcanella opened this issue May 28, 2024 · 2 comments

Comments

@pcanella
Copy link

I'm trying to run Cypress tests on Browserstack, but keep getting this. I'm on Cypress 13.6.4 and [email protected]

Not sure how to fix this ESM/CJS issue. Here's my cypress.config.ts

import task from "@cypress/code-coverage/task";
import { allureCypress } from "allure-cypress/reporter";
import { defineConfig } from "cypress";
import cypressSplit from "cypress-split";

export default defineConfig({
  e2e: {
    specPattern: "cypress/e2e/**",
    supportFile: "cypress/support/index.js",
    projectId: "test",
    video: true,
    chromeWebSecurity: false,
    reporter: "cypress-multi-reporters",
    screenshotsFolder: "./cypress/report/mochawesome-report/assets/screenshots",
    reporterOptions: {
      configFile: "reporter-config.json"
    },
    numTestsKeptInMemory: 0,
    retries: {
      runMode: 1,
      openMode: 0
    },
    viewportWidth: 1920,
    viewportHeight: 1920,
    defaultCommandTimeout: 40000,
    requestTimeout: 30000,
    pageLoadTimeout: 40000,
    setupNodeEvents(on, config) {
      task(on, config);

      cypressSplit(on, config);
      // allureWriter(on, config);
      allureCypress(on, {
        resultsDir: "./allure-results"});
      on("before:browser:launch", (browser, launchOptions) => {
        launchOptions.args.push("--another-arg");
        if (browser.family === "chromium" && browser.name !== "electron") {
          launchOptions.args.push("--disable-dev-shm-usage");
        }
        return launchOptions;
      });
      return config;
    }
  }
});
/Users/pcanella/.nvm/versions/node/v18.19.0/lib/node_modules/browserstack-cypress-cli/bin/helpers/requireModule.js:7
const mod = require(moduleName)
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/pcanella/gitrepo/feecalculatorfe/tmpBstackCompiledJs/cypress.config.js from /Users/pcanella/.nvm/versions/node/v18.19.0/lib/node_modules/browserstack-cypress-cli/bin/helpers/requireModule.js not supported.
cypress.config.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead either rename cypress.config.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in /Users/pcanella/gitrepo/feecalculatorfe/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

    at Object.<anonymous> (/Users/pcanella/.nvm/versions/node/v18.19.0/lib/node_modules/browserstack-cypress-cli/bin/helpers/requireModule.js:7:13) {
  code: 'ERR_REQUIRE_ESM'
}

@Prakash21k
Copy link

This seems to be a dependency issue, you can clone our sample repo and try running a sample project and use the same set of dependency and structure in your project file.

If this could not solve the issue please create a support ticket with Browserstack through this Contact Us page

@helgenlechner
Copy link

helgenlechner commented Sep 5, 2024

@Prakash21k The sample repo doesn't have this problem, because it doesn't have "type": "module" set in package.json. We're also running in this issue and are having to do a big workaround, because browserstack-cypress-cli is not compatible with these modern setups, which are used by Vite projects, for example.

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

No branches or pull requests

3 participants