cypress: Unable to compile TypeScript with "moduleResolution": "bundler" option in v5
Current behavior
Originally reported by @MasonM in #26308 and closed because of Cypress v13.0.0 - but this release does not fix this
cc @jennifer-shehane @lmiller1990 - ideally #26308 would be unlocked and reopened, if you can do that
Copied issue below:
If you have a TypeScript project using the new “moduleResolution”: “bundler” setting introduced in TypeScript 5, any attempts to run tests will cause the following error:
TSError: ⨯ Unable to compile TypeScript:
error TS5095: Option 'bundler' can only be used when 'module' is set to 'es2015' or later.
Desired behavior
Tests run successfully
Test code to reproduce
Reproduction 1
Courtesy of @MattyBalaam: https://github.com/MattyBalaam/cypress-ts-import
Reproduction 2
See error message and tsconfig.json below:
Your configFile is invalid: /home/runner/work/project/cypress.config.ts
It threw an error when required, check the stack trace below:
TSError: ⨯ Unable to compile TypeScript:
error TS5095: Option 'bundler' can only be used when 'module' is set to 'es2015' or later.
at createTSError (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/ts-node/dist/index.js:311:16)
at reportTSError (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/ts-node/dist/index.js:314:23)
at /home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/ts-node/dist/index.js:686:17
at Object.compile (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/ts-node/dist/index.js:743:35)
at Module.m._compile (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/ts-node/dist/index.js:856:)
at Module._extensions..js (node:internal/modules/cjs/loader:1252:10)
at Object.require.extensions.<computed> [as .ts] (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/ts-node/dist/index.js:859:16)
at Module.load (node:internal/modules/cjs/loader:1076:32)
at Function.Module._load (node:internal/modules/cjs/loader:911:12)
at Module.require (node:internal/modules/cjs/loader:1100:19)
at require (node:internal/modules/cjs/helpers:108:18)
at loadFile (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_require_async_child.js:89:14)
at EventEmitter.<anonymous> (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/child/run_require_async_child.js:116:)
at EventEmitter.emit (node:events:513:28)
at EventEmitter.emit (node:domain:9:12)
at process.<anonymous> (/home/runner/.cache/Cypress/13.1.0/Cypress/resources/app/node_modules/@packages/server/lib/plugins/util.js:33:22)
Test run failed, code 1
tsconfig.json
{
"$schema": "https://json.schemastore.org/tsconfig",
"display": "UpLeveled Node + React TSConfig",
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ESNext"],
"module": "ESNext",
"target": "ES2015",
"moduleResolution": "Bundler",
"resolveJsonModule": true,
"esModuleInterop": true,
"isolatedModules": true,
"allowJs": true,
"allowSyntheticDefaultImports": true,
"downlevelIteration": true,
"forceConsistentCasingInFileNames": true,
"jsx": "preserve",
"noEmit": true,
"noFallthroughCasesInSwitch": true,
"skipLibCheck": true,
"strict": true,
"incremental": true,
"noUncheckedIndexedAccess": true
},
"include": [
"**/.eslintrc.cjs",
"next-env.d.ts",
"**/*.ts",
"**/*.tsx",
"**/*.js",
"**/*.jsx",
"**/*.cjs",
"**/*.mjs"
],
"exclude": ["node_modules", "build", "public", "scripts"]
}
Cypress Version
13.1.0
Node version
v18.17.1
Operating System
macOS Ventura 13.5 (22G74)
Debug Logs
No response
Other
No response
About this issue
- Original URL
- State: closed
- Created 10 months ago
- Reactions: 20
- Comments: 21 (11 by maintainers)
Commits related to this issue
- module & moduleResolution needs to be NodeNext to run & build but has conflicts with jest -> https://github.com/cypress-io/cypress/issues/27731 — committed to GeorgeNonis/chatgpt-imitation by GeorgeNonis 7 months ago
- Set up cypress Cypress type declaration workaround: https://github.com/cypress-io/cypress/issues/27731#issuecomment-1715685684 — committed to John-Yip-HK/realworld by John-Yip-HK 7 months ago
- Docs: Add App Router Testing Guides and update /examples (#59268) This PR updates the testing guides to use App Router and TypeScript, also updates `/examples` to show `app` and `pages` examples. ... — committed to vercel/next.js by delbaoliveira 7 months ago
- Docs: Add App Router Testing Guides and update /examples (#59268) This PR updates the testing guides to use App Router and TypeScript, also updates `/examples` to show `app` and `pages` examples. ... — committed to agustints/next.js by delbaoliveira 7 months ago
- fix(cicd): Add env var to cypress github action https://github.com/cypress-io/cypress/issues/27731#issuecomment-1884893217 — committed to tonytino/mtggrizzly by tonytino 6 months ago
I encountered this problem today while setting up a Next.JS project. To confirm the issue I created a greenfield project to test it.
Workaround
For anyone looking for a workaround who lands here before they see the other related issues (now closed), here is a workaround copied from issue #27448:
Recreating the issue in a new project
Here are my steps and results. I hope this is helpful for whoever ends up working on a resolution.
System info
OS: macOS 14.0 Beta (23A5337a) node: v20.6.1 (I first encountered the issue on 18, then upgraded and tried again) npm: v9.8.1
Steps to reproduce
1. Create a next app using
npx2. Enter the next app and install Cypress via NPM
3. Install cypress
4. Run
cypress openSystem information after installing Cypress
Output from
% npm run cypress version:Output from
npm run cypress --version:Output from
% npm run cypress info:+1 on this. I am having the same issue.
This needs to be resolved as a fresh default installation of Next.js
14.0.1withalso causes the error
when running Cypress
13.4.0.I am able to reproduce this issue error with the slimmed types configuration:
I have only been able to produce this error when the Cypress configuration is using typescript, i.e.
cypress.config.ts. When other files (src, test files) are using.tsit seems to be working as expected.I do not see this error when the Cypress configuration is
cypress.config.js, which is what theconfig-with-module-resolution-bundlersystem test project was leveraging.The
config-with-module-resolution-bundlerproject was not using the correct types version and was testing a.jsspec, so it wasn’t actually running anything through the ts compiler to indicate the wrong version was installed given the configuration provided. So it was a false-positive test. Sorry guys!I have been able to make a few tweaks to our setup and it seems resolves the
moduleResolution: bundlererror with the reproduction I have. I want to do some more testing locally, but hoping to have something ready quick!Also facing the same issue on next.js 14. “moduleResolution”: “node” fixes it but it can have side effects on Next.js code.
@AtofStryker I upgraded to
https://cdn.cypress.io/beta/npm/13.6.3/linux-x64/fix/set_module_resolution_with_commonjs-0481c7af5d4085004b91dd644d7a4c8d97ea1b00/cypress.tgz(linux x64version from the commit) and can confirm that this is allowingmoduleResolution: "Bundler"in ourtsconfig.json🎉package.jsonI was able to remove my workaround lines below from
tsconfig.json:tsconfig.json@AtofStryker FYI, I gave this a try and it resolved the issue for me.
@ahmadbilaldev @arbuznik can either of you try
TS_NODE_COMPILER=1 npx cypress openas a workaround?Does this issue means it’s not possible to use cypress with Next.js?
Or is there a working workaround?
“next”: “13.5.6”, “cypress”: “^13.6.1”, “typescript”: “^5”
So in Cypress 13.5.0 we still need to use workaround with ts-node? Cypress complains on usage of “module”: “ESNext”, “moduleResolution”: “Bundler”,
Okay - while working through how any changes would impact the existing expectations, I have found a few possible option that may work for a few of you depending on your current setup…
TS_NODE_SKIP_PROJECT=trueor
or
cypress.config.tsfile to be compatible with these compile options. The updates I made actually lost the types from thedefineConfighelper we provide.If anyone tries these options out and they work, that’d be helpful to know if any of these are reasonable paths forward given the various project setups someone can have with various typescript versions & needs.