cypress: Cypress fails to run when type is `module`
Current behavior
When using typescript and setting type to module in package.json cypress fails to run with

Desired behavior
Cypress runs
Test code to reproduce
The simplest config you could have like shown here https://github.com/cypress-io/cypress/issues/22795#issuecomment-1232507681 or the cypress.config.ts can have
import { defineConfig } from "cypress";
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
Cypress Version
10.9.0
Node version
v16.17.1 (fails also with v18)
Operating System
Ubuntu 22.04
Debug Logs
cypress:lifecycle:child:run_require_async_child:337341 configFile: /home/isgjevori/Documents/prog/cy/cypress.config.ts +0ms
cypress:lifecycle:child:run_require_async_child:337341 projectRoot: /home/isgjevori/Documents/prog/cy +0ms
cypress:lifecycle:child:run_require_async_child:337341 try loading /home/isgjevori/Documents/prog/cy/cypress.config.ts +23ms
cypress:lifecycle:child:run_require_async_child:337341 Loading file /home/isgjevori/Documents/prog/cy/cypress.config.ts +0ms
cypress:server:util:editors user has the following editors: [ { id: 'code', binary: 'code', name: 'Visual Studio Code' }, { id: 'vim', binary: 'vim', name: 'Vim' } ] +282ms
cypress:server:saved_state making saved state from /home/isgjevori/.cache/Cypress/10.9.0/Cypress/resources/app/packages/server +211ms
cypress:server:saved_state missing project path, looking for project here +0ms
cypress:lifecycle:child:run_require_async_child:337341 User is loading an ESM config file +2ms
cypress:lifecycle:child:run_require_async_child:337341 importing esm file file:///home/isgjevori/Documents/prog/cy/cypress.config.ts +0ms
cypress:server:saved_state state path for global mode +2ms
cypress:server:appdata path: /home/isgjevori/.config/Cypress/cy/production/projects/__global__/state.json +214ms
cypress:server:saved_state full state path /home/isgjevori/.config/Cypress/cy/production/projects/__global__/state.json +1ms
cypress:server:util:editors saved preferred editor: undefined +7ms
cypress:server:util:editors return available editors: [ { id: 'computer', name: 'File System', binary: 'computer' }, { id: 'code', name: 'Visual Studio Code', binary: 'code' }, { id: 'vim', name: 'Vim', binary: 'vim' } ] +0ms
cypress:server:saved_state making saved state from /home/isgjevori/.cache/Cypress/10.9.0/Cypress/resources/app/packages/server +5ms
cypress:server:saved_state missing project path, looking for project here +0ms
cypress:server:saved_state state path for global mode +7ms
cypress:server:appdata path: /home/isgjevori/.config/Cypress/cy/production/projects/__global__/state.json +12ms
cypress:server:saved_state full state path /home/isgjevori/.config/Cypress/cy/production/projects/__global__/state.json +0ms
cypress:launcher:linux firefox is running as a snap, changing profile path +0ms
cypress:launcher:linux Could not extract version from stdout using regex: { stdout: '', versionRegex: /^Mozilla Firefox ([^\sab]+)$/m } +0ms
cypress:lifecycle:child:run_require_async_child:337341 error loading file via native Node.js module loader exports is not defined in ES module scope +53ms
cypress:lifecycle:ProjectConfigIpc error loading config {
name: 'Error',
message: 'Your \x1B[33mconfigFile\x1B[39m is invalid: \x1B[94m/home/isgjevori/Documents/prog/cy/cypress.config.ts\x1B[39m\n' +
'\n' +
'It threw an error when required, check the stack trace below:',
stack: 'ReferenceError: exports is not defined in ES module scope\n' +
' at file:///home/isgjevori/Documents/prog/cy/cypress.config.ts:2:23\n' +
' at ModuleJob.run (node:internal/modules/esm/module_job:193:25)\n' +
' at async Promise.all (index 0)\n' +
' at async ESMLoader.import (node:internal/modules/esm/loader:533:24)\n' +
' at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)\n' +
' at async loadFile (/home/isgjevori/.cache/Cypress/10.9.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_require_async_child.js:106:14)\n' +
' at async EventEmitter.<anonymous> (/home/isgjevori/.cache/Cypress/10.9.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_require_async_child.js:116:32)',
type: 'CONFIG_FILE_REQUIRE_ERROR',
details: 'ReferenceError: exports is not defined in ES module scope\n' +
' at file:///home/isgjevori/Documents/prog/cy/cypress.config.ts:2:23\n' +
' at ModuleJob.run (node:internal/modules/esm/module_job:193:25)\n' +
' at async Promise.all (index 0)\n' +
' at async ESMLoader.import (node:internal/modules/esm/loader:533:24)\n' +
' at async importModuleDynamicallyWrapper (node:internal/vm/module:438:15)\n' +
' at async loadFile (/home/isgjevori/.cache/Cypress/10.9.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_require_async_child.js:106:14)\n' +
' at async EventEmitter.<anonymous> (/home/isgjevori/.cache/Cypress/10.9.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_require_async_child.js:116:32)',
isCypressErr: true,
messageMarkdown: 'Your `configFile` is invalid: `/home/isgjevori/Documents/prog/cy/cypress.config.ts`\n' +
'\n' +
'It threw an error when required, check the stack trace below:',
originalError: [Object]
} +334ms
### Other
_No response_
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 16 (7 by maintainers)
I still had this problem in a project I was upgrading from parcel 1 to Vite, with the newer tsconfig structure (tsconfig, tsconfig.app, tsconfig.node). I tried adding the cypress files to the include of both app and node (individually, not at once) and creating a tsconfig.cypress. While I got a tsconfig.cypress working at one point, my actual project no longer ran, so I reverted the change. The only thing that actually worked was removing type: “module” from package.json.
At some point I had an issue that someone had posted a solution to of type: “module”, but now the project (and cypress) works fine without that. However, it would be good if there was better information about how to make this work with a main tsconfig that’s just references.
@StephaneColson I reproduced it. I was able to get it to work by updating tsconfig.json:
Docs for
module. This is also described in the comment prior to yours.By default, it’s CommonJS for
"module": "commonjs", which isn’t compatible withtype=module.I could be misunderstanding something, but I think this is the correct behavior -
type: moduleconflicts with thetsconfig.jsonyou’ve provided (which defaults to CommonJS).Any update on this? I tried
yarn patch cypressto see if I could patch the offending code for my project, but I’m having trouble sorting out the source code with what gets installed in my project locally.@MateusNeres26 thanks, I was able to reproduce it with this project.
The only things that fixed this for me in a new vite project were: removing
"type": "module"from mypackage.json;or renamingcypress.config.tstocypress.config.mts.The renaming fix actually didn’t work for me. Every time I start up cypress after renaming the config file it tries to go through the automated configuration process again and creates a new
cypress.config.tsinstead of reading fromcypress.config.mts.However, renaming the config file to
cypress.config.jsdid work and allowed me to keep"type": "module"in mypackage.json.@lmiller1990 Your suggestion fixed it for me.
FYI: Adding the following line to
tsconfig.jsonfixed this for me:My full
tsconfig.jsonlooks like this (don’t know if all of this makes sense but at least it works for me):https://github.com/JoanEsquivel/cypress-cucumber-boilerplate
We had a very similar problem. Ended up with renaming
cypress.config.tstocypress.config.cjs, and it worked! I guess this is something which should be handled by cypress itself. And according to the following it should…https://github.com/cypress-io/cypress/issues/23552