Skip to content

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module error thrownΒ #8090

@nitzanashi

Description

@nitzanashi

Current behavior:

We're trying to update Cypress from 4.5.0 to >= 4.6.0 but we encountered into a problem

Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/nitzannashi/zattoo/web/projects/common/cypress/plugins/index.js
require() of ES modules is not supported.
require() of /Users/nitzannashi/zattoo/web/projects/common/cypress/plugins/index.js from /Users/nitzannashi/zattoo/web/projects/common/cypress/plugins/index.cjs is an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which defines all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use import(), or remove "type": "module" from /Users/nitzannashi/zattoo/web/package.json.

    at Module._extensions..js (internal/modules/cjs/loader.js:1216:13)
    at Object.require.extensions.<computed> [as .js] (/Users/nitzannashi/Library/Caches/Cypress/4.7.0/Cypress.app/Contents/Resources/app/packages/server/node_modules/ts-node/src/index.ts:539:12)
    at Module.load (internal/modules/cjs/loader.js:1049:32)
    at Function.Module._load (internal/modules/cjs/loader.js:937:14)
    at Module.require (internal/modules/cjs/loader.js:1089:19)
    at require (internal/modules/cjs/helpers.js:73:18)
    at /Users/nitzannashi/zattoo/web/projects/common/cypress/plugins/index.cjs:22:54
    at processTicksAndRejections (internal/process/task_queues.js:97:5)

My setup is a little bit different from the usual one, I have a index.cjs and index.js, cypress.json config is as follow:

    "nodeVersion": "system",
    "pluginsFile": "projects/common/cypress/plugins/index.cjs",

plugins/index.cjs

module.exports = (...args) => {
    return import('./index.js').then((mod) => {
        return mod.default(...args);
    });
};

plugins/index.js

import snapshotPlugin from 'cypress-image-snapshot/plugin.js';
import retriesPlugin from 'cypress-plugin-retries/lib/plugin.js';
import webpack from '@cypress/webpack-preprocessor/index.js';

const plugins = async (on, config) => {
    ....
}

export default plugins;

package.json as "type": "module", but I do not understand why cypress should care about it.

Furthermore I tried to update @cypress/webpack-preprocessor from 4.1.3 -> 5.4.0 with modification to index.js but no changes.
I'm not sure if it's a cypress issue or cypress/webpack-preprocessor issue but it's a blocker for me to update cypress

Desired behavior:

Cypress is loaded as expected and working

Versions

>= 4.6.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    prevent-stalemark an issue so it is ignored by stale[bot]type: bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions