question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error [ERR_REQUIRE_ESM]: require() of ES Module during Cypress upgrade to the v10.0.1

See original GitHub issue

Current behavior

When I try to upgrade Cypress to v10.0.1 I get “SyntaxError: Cannot use import statement outside a module”

Your configFile is invalid: /Users/xxx/Documents/Cypress/my_project/cypress.config.js

It threw an error when required, check the stack trace below:`

`/Users/xxx/Documents/Cypress/my_project/cypress.config.js:1
import { defineConfig } from "cypress";
^^^^^^

SyntaxError: Cannot use import statement outside a module
    at Object.compileFunction (node:vm:352:18)
    at wrapSafe (node:internal/modules/cjs/loader:1033:15)
    at Module._compile (node:internal/modules/cjs/loader:1069:27)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Module.require (node:internal/modules/cjs/loader:1005:19)
    at require (node:internal/modules/cjs/helpers:102:18)
    at loadFile (/Users/xxxi/Library/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_require_async_child.js:103:14)
    at EventEmitter. (/Users/xxx/Library/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_require_async_child.js:146:38)
    at EventEmitter.emit (node:events:527:28)
    at EventEmitter.emit (node:domain:475:12)
    at process. (/Users/xxx/Library/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/util.js:33:22)
    at process.emit (node:events:527:28)
    at process.emit (node:domain:475:12)
    at emit (node:internal/child_process:938:14)

I’ve read that the solution for this issue is adding "type": "module" in package.json but now I am getting another issue:

101 | 
  102 |     try {
> 103 |       return require(file)
      |              ^
  104 |     } catch (err) {
  105 |       originalError = err
  106 |       if (!err.stack.includes('[ERR_REQUIRE_ESM]') && !err.stack.includes('SyntaxError: Cannot use import statement outside a module')) {
  107 |         throw err

[Stack trace](http://localhost:55743/__launchpad/index.html#) Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/xxx/Documents/Cypress/my_project/cypress.config.js from /Users/xxx/Library/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_require_async_child.js not supported. Instead change the require of cypress.config.js in /Users/xxx/Library/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_require_async_child.js to a dynamic import() which is available in all CommonJS modules. at loadFile (/Users/xxx/Library/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_require_async_child.js:103:14) at EventEmitter. (/Users/xxxLibrary/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/child/run_require_async_child.js:146:38) at EventEmitter.emit (node:events:527:28) at EventEmitter.emit (node:domain:475:12) at process. (/Users/xxxLibrary/Caches/Cypress/10.0.1/Cypress.app/Contents/Resources/app/packages/server/lib/plugins/util.js:33:22) at process.emit (node:events:527:28) at process.emit (node:domain:475:12)

Do you have any ideas on how to solve it?

Cypress Version

10.0.1

Package Manager

npm

Operating system

Mac

Other

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:3
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
lmiller1990commented, Jun 2, 2022

Are you able to share your existing project? Ideally a repo we could clone, but if that’s too much trouble, a package.json, cypress.json and cypress/plugins/index.js would be a good start.

If you could re-run the migration with DEBUG=cypress* and include the logs from the terminal, that would help, too!

It looks like it’s incorrectly generating a ES modules based config; if you just want to make it work, you could change to using CJS:

const { defineConfig } = require('cypress')

module.exports = defineConfig({
  // your config content here, as generated by the migration tool
})

If you could share a slimmed down version of the repo you want to migrate, that’d be great - we can work through it and find out what’s going wrong, so others can have a smooth migration process.

0reactions
cypress-bot[bot]commented, Jun 2, 2022

Released in 10.0.2.

This comment thread has been locked. If you are still experiencing this issue after upgrading to Cypress v10.0.2, please open a new issue.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error [ERR_REQUIRE_ESM]: require() of ES Module not ...
The node-fetch latest version doesn't use the require() syntax to import the package. You need to go to your package.json and type
Read more >
Migration Guide | Cypress Documentation
Migrating to Cypress 12.0 This guide details the changes and how to change your code to migrate to Cypress version 12.0.
Read more >
Error [ERR_REQUIRE_ESM]: Must use import to load ES ...
When starting node-red I am getting following error in log file: Welcome to ... require() of ES modules is not supported. require() of ......
Read more >
v11 Migration Guide - Nx
Migrating to Cypress V11. Cypress v10 introduce new features, like component testing, along with some breaking changes. Nx can help you migrate from...
Read more >
Migrating to Cypress 10 - step by step tutorial - YouTube
Step by step tutorial to upgrade your framework to Cypress 10.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found