Closed nwalters512 closed 2 years ago
I'd expect this to work, too. We should fix this. Thanks for the excellent bug report.
Actually fixed by https://github.com/cypress-io/cypress/pull/23637 - just tested it out. Going to close this one off, it'll be in the next release.
Adding a quick test: https://github.com/cypress-io/cypress/pull/23695
I get this during the installation of cypress, with the created config file.
Can you share a minimal reproduction? It's likely a configuration error - the error isn't very insightful, but I can help you fix it once I know your setup.
@oliviervanbulck (or anyone else coming here) you might want to also see https://github.com/cypress-io/cypress/issues/24111 which has some common solutions to this issue (usually type=module conflicting with the defaults in tsconfig.json
).
Can't share a reproduction, but I am also having the issue of a newly installed cypress
not working if "type":"module"
is set in package.json
Running Node both 18.14, tried 16.19 and "cypress": "^12.7.0"
Can't share a reproduction, but I am also having the issue of a newly installed
cypress
not working if"type":"module"
is set inpackage.json
Running Node both 18.14, tried 16.19 and
"cypress": "^12.7.0"
I can confirm that
"type":"module"
cypress.config.ts
to cypress.config.js
Resolve the issue
I can confirm that we have exactly the same issue when setting type:module in package.json. Maybe someone could explain why changing cypress.config.ts
to cypress.config.js
makes everything work?
If you use a .js
extension with type: module"
it will use Node.js and the default ESM loader. To clarify, type: module
means "I am using ESM (ES modules)" - the one with import
and export
.
If you are using .ts
, it won't use Node.js and the default ESM loader -- Node.js, out of the box, only works with JavaScript, and doesn't know how to handle other extensions or languages (like TypeScript).
In this case, we use ts-node
. ts-node
ships with several loaders - the ones we use are here. There's two - the default which works with CJS (CommonJs modules, eg module.exports
and require
) and ESM (import
/ export
).
The conditional logic to decide which loader we use is here.
In your case, I suspect the issue is related to your tsconfig.json
, possibly https://github.com/cypress-io/cypress/issues/24111#issuecomment-1445726935. What could be happening is we are using the ESM loader, but your tsconfig.json
is compiling the code to CommonJS modules under the hood, which is giving the error you are encountering. https://github.com/cypress-io/cypress/issues/24111#issuecomment-1445726935 might help.
Alternatively, you can do a ts-node
specific change like this in your tsconfig.json
:
{
// Most ts-node options can be specified here using their programmatic names.
"ts-node": {
// It is faster to skip typechecking.
// Remove if you want ts-node to do typechecking.
"transpileOnly": true,
"compilerOptions": {
// compilerOptions specified here will override those declared below,
// but *only* in ts-node. Useful if you want ts-node and tsc to use
// different options with a single tsconfig.json.
"module": "ES2015", // <==== added this, so it will compile to ES modules, not CommonJS
}
}
}
The reason this stuff is so confusing is many projects have a single tsconfig.json
shared by multiple tools (eg, frontend bundler, server code, and Cypress).
I hope this comment clarifies a bit around what's actually happening, and helps fix your issue. Let me know if you've got any other ongoing issues.
Proposed fixes do not work for me, I don't want to remove "type": "module"
and the ts-node
patches results in an empty stacktrace. I guess I'll go for another e2e library.
I am getting this with Cypress 13.12.0, using the default generated file in a scaffolded Vue app (using Vite).
tsconfig.node.js
:
{
"extends": "@tsconfig/node20/tsconfig.json",
"include": [
"vite.config.*",
"vitest.config.*",
"cypress.config.*",
"nightwatch.conf.*",
"playwright.config.*"
],
"compilerOptions": {
"composite": true,
"noEmit": true,
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"module": "ESNext",
"moduleResolution": "Bundler",
"types": ["node"]
}
}
cypress.config.ts
:
import { defineConfig } from 'cypress';
export default defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
},
});
My package.json
does include type: module
.
Changing the file to a .js
extension does fix the problem without needing to remove type:module
. I would rather keep it as a .ts
file though if there's a fix?
This is still a problem in the latest cypress 13.13.0
❗ Had to rename .ts
config to the .js
as joshrogers857 mentioned above.
Same issue as @Vondry. For cypress 13.13.0. Renamed to cypress.config.mjs
and preserved type: "module"
in package.json worked. Renaming to .js didn't
I got this working with yarn cypress open --config-file cypress.config.mts
. I just switched the configuration to a .mts
file and it ran just fine. This is with "type": "module"
in a ViteJS project. Just incase someone else ends up seeing this.
If this can help anyone, I fixed this issue by simply setting "module": "ESNext"
in my tsconfig.json
Current behavior
I would expect Cypress to work out of the box with TypeScript and ESM:
"type": "module"
inpackage.json
tsconfig.json
:The following
cypress.config.ts
:However, with that setup, Cypress currently fails with the following error:
Desired behavior
Cypress should work with this setup.
Test code to reproduce
Clone https://github.com/nwalters512/cypress-esm-typescript-config, then run
yarn && yarn cypress
. Observe the following error message:Cypress Version
10.6.0
Node version
16.13.0
Operating System
macOS 12.5
Debug Logs