Skip to content
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

Throw a more detailed error message on require of ESM modules #2105

Closed
michael-lloyd-morris opened this issue Aug 4, 2022 · 6 comments
Closed
Labels
🏦 debt Tech debt ✅ accepted The core team has agreed that it is a good idea to fix this

Comments

@michael-lloyd-morris
Copy link
Contributor

🤔 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 michael-lloyd-morris added the 🏦 debt Tech debt label Aug 4, 2022
@davidjgoss davidjgoss added the ✅ accepted The core team has agreed that it is a good idea to fix this label Aug 5, 2022
@mattwynne
Copy link
Member

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

@michael-lloyd-morris
Copy link
Contributor Author

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

@davidjgoss
Copy link
Contributor

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.

@Izhaki
Copy link
Contributor

Izhaki commented Mar 8, 2023

The error message is not a cucumber one. For more details and fix see: #2248 (comment)

@davidjgoss
Copy link
Contributor

@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.

@michael-lloyd-morris
Copy link
Contributor Author

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

#2264

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🏦 debt Tech debt ✅ accepted The core team has agreed that it is a good idea to fix this
Projects
None yet
Development

No branches or pull requests

4 participants