cucumber-js icon indicating copy to clipboard operation
cucumber-js copied to clipboard

Throw a more detailed error message on require of ESM modules

Open michael-lloyd-morris opened this issue 2 years ago • 3 comments

🤔 What's the problem you've observed?

I was stumped for about a full day by using the require directive in my configuration when I should have been using the import directive. Let's try to make the error message that the user sees more effective in pointing the user in the correct direction.

✨ Do you have a proposal for making it better?

Current error message

Error [ERR_REQUIRE_ESM]: require() of ES Module <step> from <project-root>\node_modules\@cucumber\cucumber\lib\api\support.js not supported.
index.js is treated as an ES module file as it is a .js file whose nearest parent package.json contains "type": "module" which declares all .js files in that package scope as ES modules.
Instead rename index.js to end in .cjs, change the requiring code to use dynamic import() which is available in all CommonJS modules, or change "type": "module" to "type": "commonjs" in C:\Users\tendo\Projects\ucx\mfa\tests\cucumber-screenplay\package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead).

If we can catch that error we can throw this in it's place.

Error [ERR_REQUIRE_ESM]: require() of ES Module encountered. Use the import configuration directive instead of require.
Example:  cucumber-js --import steps.js

Maybe even go so far as to point out the exact code change needed?

michael-lloyd-morris avatar Aug 04 '22 17:08 michael-lloyd-morris

Nice idea! Do you want to pair on fixing it? You can book me at https://calendly.com/mattwynne

mattwynne avatar Aug 05 '22 17:08 mattwynne

I'd love to, but I've got to dig up some time. I'll schedule something later in the week.

michael-lloyd-morris avatar Aug 15 '22 13:08 michael-lloyd-morris

In a similar vein it would make sense to emit a warning when requireModule is used at the same time as import because that's probably not going to work.

davidjgoss avatar Aug 18 '22 14:08 davidjgoss

The error message is not a cucumber one. For more details and fix see: https://github.com/cucumber/cucumber-js/issues/2248#issuecomment-1459112907

Izhaki avatar Mar 08 '23 01:03 Izhaki

@Izhaki I'm aware of that, but it is triggered by Cucumber trying to load code and will frequently be due to misconfguration, so if we can surface that in a more useful way I think that's worthwhile.

davidjgoss avatar Mar 08 '23 03:03 davidjgoss

This issue now has a pull request so let's continue discussion in the thread of that PR.

https://github.com/cucumber/cucumber-js/pull/2264

michael-lloyd-morris avatar Mar 20 '23 21:03 michael-lloyd-morris