Project with type Modules skips prompt
kevmul opened this issue · comments
Hey everyone. I have a project that has type set to module
in the package.json
file.
We have been using Hygen for a while and we recently updated our entire app. The last thing we need to fix up is the Hygen generators.
It seems that with type: module
in the package.json
file, the prompt.js
file is just skipped. This leads to errors where the templates are looking for the specific inputs that are not being requested.
Following some other issues here and there, I have tried renaming prompt.js
to prompt.cjs
and prompt.mjs
. Both are skipped completely.
I had also tried to use export default
in place of module.exports
but all this doesn't matter as the file is skipped anyway.
Here is the error output
Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/kmul/code/temp/_templates/fake/with-prompt/prompt.js from /Users/kmul/code/temp/node_modules/hygen/dist/prompt.js not supported.
/Users/kmul/code/temp/_templates/fake/with-prompt/prompt.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 /Users/kmul/code/temp/_templates/fake/with-prompt/prompt.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 /Users/kmul/code/temp/package.json to treat all .js files as CommonJS (using .mjs for all ES modules instead)
My question is, what do I have to do to get Hygen to work with type:module
?
I added a PR to fix this. I believe the only issue is just adding a prompt.cjs
to the hooksfiles
. I would have created a test to make sure, but didn't really know how the tests work in your repo.
PR: #407
If you want a test created, let me know how / where that would be handled and I will happily create one.
This issue is now resolved and can be closed