-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Problems with transitive (pure) ESM dependency #2248
Comments
Do you have a |
Yeah sure! (Totally forgot about that, sorry) {
"compilerOptions": {
"target": "es5",
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
},
"include": [
"src"
]
} |
As you've found I don't think Calling
That would be good if you have time. |
I created a small example repository here. It seems like the issue now comes from babel. As stated here, |
I've been pretty much on the same path as yours, trying everything you've tried, and spent around 10h on this issue. It would be impossible for me to impart all I've learnt, but this is generally not a cucumber issue. Rather, it is the current friction between CJS and ESM on node (I haven't had a chance to fully investigate it, but my current hypothesis is that the The good news is that there is a working solution: https://www.npmjs.com/package/fix-esm; I've used this:
And for the brave, a hint of the nature of the issue is in that library code: if (error.code === "ERR_REQUIRE_ESM") {
// NOTE: We completely bypass the default internal loader, as we cannot patch the `type: module` check out of that. Please open a PR if you have a better solution!
let code = fs.readFileSync(filename, "utf8");
let transpiledCode = transpile(code);
mod._compile(transpiledCode.code, filename);
} else {
throw error;
} |
Yeah, exactly my thought. This whole topic of CJS and ESM is somewhat confusing, especially when coming from a different background. It would be great, if library vendors would always still include a CJS build, considering that its actually not that hard to do with rollup etc., but oh well.. I will try the fix-esm package later, seems like it's the best and most "generic" way to go about the issue. Thanks a lot for the hint! |
I'll close this now as I don't think there's anything for Cucumber to change, it's more a reflection of the weird state of things in Node.js at the moment with regard to module formats and loading. |
@davidjgoss I agree that this issue should be closed. However, I believe Although many do not support it yet (NextJS for example, as it is using internally bespoke loaders), more and more libraries phase out CJS support, and more and more people migrate to esm repros. These problems will just generate more and more noise in the future. I wonder if an initiative such as that by @halvardssm, where we split the distribution to This topic is probably worthy a new issues, maybe an RFC. |
No, we support ESM as well and will use dynamic import where specified:
I don't think that would achieve anything. It was one of the patterns we considered for dual support initially, but it achieves essentially the same result. |
More background #2059 |
Oooooooooohhhhh..... It does work! But you have to:
I have read the ESM guide before, but completely missed this sentence:
This is awesome to know! |
@Izhaki This does not seem to work with Typescript. But using What I don't like about the |
I ran into a similar issue to this with NextJS and wrote about my findings here. It might have enough overlap to help: |
Not sure whether this is a bug in cucumber-js or some misconfiguration/misunderstanding on our part.
👓 What did you see?
In a library we develop, we use two pure ESM dependencies (react-dnd and react-table-library). The library is written in typescript, so this is not a problem generally. For our unit tests with jest we us babel to transform these libraries to commonJS, which works like a charm.
However, when we import our library in a support file for cucumber we get issues, because we try to require() an ES module from our commonJS bundle. Which seems logical to me, but after many hours of trying to work around this issue, I'm stuck now.
We use babel for transpiling:
and register it in our cucumber config:
AFAIK the
@babel/register
only registers the on-the-fly require-hook into node, which does not work for ESM, so execution stops at the firstrequire()
of such ESM.I also tried the following (with and without
@babel/register
):import
option withNODE_OPTIONS=--loader ts-node/esm
does not work. -> Typescript support files are loaded as CJSNODE_OPTIONS=--loader ts-node
does not work -> raisesTypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".tsx"
requireModule: ts-node/register
-> seems to be transpiled twicerequireModule: ts-node/register
-> raisesTypeError [ERR_UNKNOWN_FILE_EXTENSION]: Unknown file extension ".tsx"
requireModule: 'ts-node/register'
does not work✅ What did you expect to see?
I expected all files to be correctly transformed by babel, before being processed by cucumber-js, just like we achieve it in jest.
📦 Which tool/library version are you using?
@cucumber/cucumber: 8.11.1
@babel/cli: 7.21.0
ts-node: 10.9.1
node: v16.16.0
🔬 How could we reproduce it?
If this is not something completely obvious, I could create an example repository for this issue.
Steps to reproduce the behavior:
📚 Any additional context?
Like I said, I'm not sure if this is a bug or just some stupidity on our side. I looked into ways to circumvent this issue at the level of our library, but I didn't find some babel or rollup config to convert the imports for only these libs to async
import()
. Converting all locations manually or (even worse) providing our own CJS ports of the libs seems overkill, considering we only have this issue with cucumber.I'd appreciate any idea on this case!
This text was originally generated from a template, then edited by hand. You can modify the template here.
The text was updated successfully, but these errors were encountered: