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

Export husky module in project defaulting to ESM requires the .default() property usage #1362

Closed
jerome-benoit opened this issue Jan 26, 2024 · 6 comments

Comments

@jerome-benoit
Copy link

jerome-benoit commented Jan 26, 2024

To reproduce:

  • Create node.js a project defaulting to ESM ("type": "module")
  • Install husky as deps dev
  • Import husky in a prepare.js ESM module and try to run it in the prepare step: import('husky').then(husky => husky()):

TypeError: husky is not a function
at file:///Users/I339261/SAPDevelop/e-mobility-charging-stations-simulator-git/prepare.js:7:33

  • And doing import('husky').then(husky => husky.default()) works

Using the default attribute on husky imported ESM module is not expected.

@jerome-benoit jerome-benoit changed the title Export husky module in project defaulting to ESM requires the .default() usage Export husky module in project defaulting to ESM requires the .default() property usage Jan 26, 2024
@typicode
Copy link
Owner

typicode commented Jan 26, 2024

Why not do import husky from 'husky' and then husky()?

By the way, docs have been updated recently regarding this. It's not using an await anymore. That may be where the confusion arised.

@jerome-benoit
Copy link
Author

jerome-benoit commented Jan 26, 2024

The issue remains the very same, however the import is done.

And husky is not installed at deployment on the cloud or CI, a full conditional control over its import is mandatory.

And whatever is the use case, a default ESM import of a function shall just work.

The root cause is that the exports fields miss types et import fields. And since husky seems to be ESM only now, properly unsupport commonjs.

@typicode
Copy link
Owner

Thank you I forgot the reason for the await. I'll have a look.

@jerome-benoit
Copy link
Author

I think node.js has CLI arguments to enforce ESM module resolution to cook a unit test.

@typicode
Copy link
Owner

I've done some quick search and I think it's normal to have to use default() when using a dynamic import:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import#module_namespace_object

with the default export available as a key called default

@jerome-benoit
Copy link
Author

Fair enough. I guess it's then just a matter of updating the documentation and/or the release notes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants