cucumber-js
cucumber-js copied to clipboard
Throw a more detailed error message on require of ESM modules
🤔 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?
Nice idea! Do you want to pair on fixing it? You can book me at https://calendly.com/mattwynne
I'd love to, but I've got to dig up some time. I'll schedule something later in the week.
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.
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 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.
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