Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

vee-validate loags as CommonJS module in ESM context with latest version #4905

Closed
1 of 5 tasks
aKzenT opened this issue Oct 22, 2024 · 3 comments
Closed
1 of 5 tasks
Labels
🐛 bug Unintended behavior

Comments

@aKzenT
Copy link

aKzenT commented Oct 22, 2024

What happened?

The latest vee-validate version uses the exports field to declare its entry points. Our project uses ESM and loaded vee-validate as ESM with the previous version. With the latest version we get the following error however:

Named export 'configure' not found. The requested module 'vee-validate' is a CommonJS module, which may not support all module.exports as named exports. CommonJS modules can always be imported via the default export, for example using: import pkg from 'vee-validate'; const { useForm, configure, defineRule, useField } = pkg;

I believe this issue is caused by a combination of various factors:

  1. exports declaration always have priority over other package.json properties like "main" or "module".
  2. exports declarations do not declare the format of the target file. they only target how the package was loaded (e.g. by require or import)
  3. all vee-validate files use the .js file extension, regardless of whether they are in CJS or ESM format
  4. vee-validate does not specify a "type" in package.json
  5. therefore, the .js files are treated as CJS, even if they are ESM files

This is in contrast to the previous version, where the "module" property implicitly marks the target as being an ESM file.

I believe the correct solution is to specify a default type in the "type" field and use explicit file extensiosn (.cjs, .mjs) for the other type. This approach is also documented on the official NodeJS documentation: https://nodejs.org/api/packages.html#dual-commonjses-module-packages

Further resources:
https://stackoverflow.com/a/77287842
https://nodejs.org/api/esm.html#modules-ecmascript-modules
https://nodejs.org/api/packages.html#dual-commonjses-module-packages

Reproduction steps

...

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

No response

Demo link

none

Code of Conduct

poissoj added a commit to poissoj/Dossier-Facile-Frontend that referenced this issue Oct 23, 2024
@logaretm logaretm added the 🐛 bug Unintended behavior label Oct 23, 2024
@logaretm
Copy link
Owner

logaretm commented Oct 23, 2024

Didn't realize that the names are required to be ending with .cjs or .mjs. Would just adding type and set it to module in package.json work?

I cannot reproduce this issue in my Vite or Nuxt setup, is there an environment where this can be reproduced/observed?

EDIT: I can reproduce in a simple Node.js environment running .mjs and .cjs imports.

@logaretm
Copy link
Owner

Should be fixed with v4.14.4

@aKzenT
Copy link
Author

aKzenT commented Oct 24, 2024

can confirm that v4.14.4 fixes the issue for us. Thank you very much for the fast response!

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
🐛 bug Unintended behavior
Projects
None yet
Development

No branches or pull requests

2 participants