New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loading .mjs file or esmodule dependency in cypress config #23540
Comments
Thanks for the reproduction. We will need to look into this - is it actually valid in the TS compiler to write I'm not sure TS actually does module transpiling - you'd normally need webpack for something this like, but I could be mistaken here. |
The |
Thanks for the updating reproduction. Just to clarify... cy.task is for executing code in Node. It looks like Konva is canvas drawing libary for the browser - do you have a particular goal in mind for executing this in Node? I looked at their README, specifically here: https://github.com/konvajs/konva#commonjs-modules. I tried their recommendation to use the const { Canvas } = require('konva/cmj/Canvas');
const { Context } = require('konva/cmj/Context');
export class DrawableObject {
constructor(
private canvas = new Canvas({}),
private konvaContext = new Context(canvas)
) {}
draw() {
this.konvaContext.closePath();
}
} We use ## My file
$ cat index.ts
import * as Blah from "./blah.mjs"
console.log(Blah)
## Try it out
$ yarn ts-node index.ts
$ /Users/lachlan/code/dump/ts/node_modules/.bin/ts-node index.ts
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/lachlan/code/dump/ts/blah.mjs not supported.
Instead change the require of /Users/lachlan/code/dump/ts/blah.mjs to a dynamic import() which is available in all CommonJS modules.
at Object.<anonymous> (/Users/lachlan/code/dump/ts/index.ts:3:12)
at Module.m._compile (/Users/lachlan/code/dump/ts/node_modules/ts-node/dist/index.js:857:29)
at Object.require.extensions.<computed> [as .ts] (/Users/lachlan/code/dump/ts/node_modules/ts-node/dist/index.js:859:16)
at phase4 (/Users/lachlan/code/dump/ts/node_modules/ts-node/dist/bin.js:466:20)
at bootstrap (/Users/lachlan/code/dump/ts/node_modules/ts-node/dist/bin.js:54:12)
at main (/Users/lachlan/code/dump/ts/node_modules/ts-node/dist/bin.js:33:12)
at Object.<anonymous> (/Users/lachlan/code/dump/ts/node_modules/ts-node/dist/bin.js:579:5) {
code: 'ERR_REQUIRE_ESM'
} For now, I think your best option is using their The reason this does work in your production build is webpack, unlike TypeScript, does bundle "everything" including making all the different module formats work together. Hopefully this gives some insight into what's actually going on here - still curious on the use case of a Canvas rendered on the Node end. |
I am not trying to run Konva in node. I am trying to import a class from our production app that happens to be importing a class from Konva (and I'm doing that just so I can use the class as a TypeScript type, it's not actually used at runtime, but since it's a class the import sticks around)). This problem is not unique to Konva but would happen with any library that uses esnext. As far as I know there is no way to determine whether a node package uses
I thought that if you tell TypeScript to include a particular node module in its compilation (ie. via the I guess if ts-node doesn't support it, it would be hard to make Canvas support it. Closing this issue then, sounds like it's more of a problem with ts-node than Cypress. |
This is the confusing part for sure - my understanding is TS won't transform ESM to CJS (source of your bug). It's a bit confusing, since TS does give the appearance of doling some module transforms. Here's an example (and how I usually debug this - might be useful for other people googling this issue).
|
Current behavior
I have some data seeding code that was imported into a Cypress test. I am now moving that code into a
cy.task
, thus trying to import via node. Once I did that I started seeing this error:I eventually figured out that one of our dependencies uses
type: "module"
in itspackage.json
. Our app does not use esmodules, instead it is TypeScript that gets transpiled down to use commonjs. #22118 makes cypress useawait import
which breaks in our case since we aren't using esmodules.Desired behavior
I would expect that Cypress can load any files in the config file that we would import into a test. I realize these are two completely separate things; one is bundled and the other is not. However, this difference is not really visible to the end user and therefore it creates a bad DX to not be able to import a file in
cypress.config.ts
that I can import just fine in a test.Test code to reproduce
https://github.com/joefiorini/cypess-esmodule-import-error/tree/main
Cypress Version
10.6.0
Node version
18.4.0
Operating System
Linux 5.15.59-2-lts
Debug Logs
No response
Other
No response
The text was updated successfully, but these errors were encountered: