lzear / votes

JS library for ranked voting systems
https://rank-votes.vercel.app
MIT License
23 stars 1 forks source link

Cannot resolve Lodash dependencies from Webpack + TypeScript #107

Open Quanyails opened 1 year ago

Quanyails commented 1 year ago

First, I just want to say thanks for publishing this library! This is a high-quality resource for running different types of voting algorithms. I'm interested in using this library for hobby purposes.


I'm trying to run this library from a Webpack config, but I'm running into error messages that resemble the following:

ERROR in ./node_modules/votes/dist/index.mjs 1:895-929
Module not found: Error: Can't resolve 'lodash/difference' in '[...]\node_modules\votes\dist'
Did you mean 'difference.js'?
BREAKING CHANGE: The request 'lodash/difference' failed to resolve only because it was resolved as fully specified
(probably because the origin is strict EcmaScript Module, e. g. a module with javascript mimetype, a '*.mjs' file, or a '*.js' file where the package.json contains '"type": "module"').
The extension in the request is mandatory for it to be fully specified.
Add the extension to the request.

The Webpack config I'm using comes from the official Webpack + TypeScript docs.

I'm able to resolve this issue by following these steps. Can we provide an out-of-the-box fix for this, though?

Additional information:

lzear commented 1 year ago

Hello @Quanyails Thanks a lot for the message! And thanks for letting me know about this issue.

Unfortunately I haven't been able to find a good solution. Possibly it could be fixed if I used lodash-es instead of lodash, but I cannot do that as I am exporting the library both as CommonJS (CJS) and as ECMAScript modules (ESM).

I think one solution can be for you to use CJS in your project. Here is a PR that does the change https://github.com/Quanyails/cap-scratchpad/pull/1. Be aware, that's maybe not something you want to do, as ESM is supposed to be "the future".

I will leave this issue open an maybe give it another attempt later.

Quanyails commented 1 year ago

Hi @lzear! 👋 Yeah, I was hoping I didn't need to switch my project to use CJS imports. If there's no elegant solution, though, I'm content to use workarounds. Thanks for looking into this issue, in any case.

lzear commented 7 months ago

Hi @Quanyails

I think I finally fixed it :sweat_smile: From version 3.0.0, your workaround is probably not needed https://github.com/lzear/votes/releases/tag/votes%403.0.0