node-formidable / formidable

The most used, flexible, fast and streaming parser for multipart form data. Supports uploading to serverless environments, AWS S3, Azure, GCP or the filesystem. Used in production.
MIT License
7.04k stars 682 forks source link

Invalid invocation of `hexoid` in formidable@2.0.1 #871

Closed mwaibel-go closed 1 year ago

mwaibel-go commented 2 years ago

Support plan

Context

What are you trying to achieve or the steps to reproduce?

I’m compiling a test suite with webpack. The tests require supertest, which through its dependency superagent relies on formidable@2.0.1.

The offending lines are lines 8 and 15 in Formidable.js,

const hexoid = require('hexoid');
// ...
const toHexoId = hexoid(25);

You appear to be relying on hexoid’s behaviour prior to this commit in which the export mechanism was changed.

[Edit: It turns out I jumped to conclusions there, as the change in that repository was not published. I’m thinking this issue might actually be related to the switch to ES modules merged in 70517da99cd1437fa32628ebd42ccb356fb286f0 (release 3.0.0), but JS modules are beyond me.]

Changing the import in line 8 to

const hexoid = require('hexoid').default;

fixes the error for me.

Unfortunately, the author of that library didn’t bump their version after the breaking change. I suggest you either released the fix I suggested above (if @^2.0.0 is still supported), or you pinned the exact git commit in your dependencies.

What was the result you got?

When I try to compile my tests using webpack, the compiler throws this Error:

TypeError: hexoid is not a function
    at Object../node_modules/formidable/src/Formidable.js ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/node_modules/formidable/src/Formidable.js:15:1)
    at __webpack_require__ ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/webpack/bootstrap:19:1)
    at Object../node_modules/formidable/src/index.js ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/node_modules/formidable/src/index.js:5:20)
    at __webpack_require__ ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/webpack/bootstrap:19:1)
    at Object../node_modules/superagent/lib/node/index.js ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/node_modules/superagent/lib/node/index.js:46:20)
    at __webpack_require__ ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/webpack/bootstrap:19:1)
    at Object../node_modules/supertest/lib/test.js ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/node_modules/supertest/lib/test.js:11:21)
    at __webpack_require__ ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/webpack/bootstrap:19:1)
    at Object../node_modules/supertest/index.js ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/node_modules/supertest/index.js:8:14)
    at __webpack_require__ ($PROJECT_DIR/dist/webpack:/$PROJECT_NAME/webpack/bootstrap:19:1)

($PROJECT_DIR and $PROJECT_NAME are replacements of my actual file path and project name.)

What result did you expect?

No compile-time error.

Thank you for your time.

tunnckoCore commented 2 years ago

Heya. @mwaibel-go

Yea, probably, that's why a massive ESM wave came last few years while I was absent. Finally we can write standard things and compile to whatever we what, properly.

I think we should support non-ESM (CJS) in v3, that's probably I'm holding moving v3 to latest (plus other stuff but yea). @GrosSacASac

const hexoid = require('hexoid').default;

Yea, that's an easy fix, I can patch it as 2.0.2 or something. Or you can PR, it's on v2-latest branch.

bleistift-zwei commented 2 years ago

Thank you for your response. I’ve looked into it, but on the v2-latest branch the test suite fails with a fresh clone. Applying the intended fix fails some 14 test cases more.

As I said, modules are really not my area of expertise, and currently I don’t have the time to read into that and another codebase to get the tests to work again. I’m sorry for that.

angusryer commented 1 year ago

For anyone coming to this with failures building using webpack or other build systems that use webpack under the hood, these threads have some successful fixes:

https://github.com/node-formidable/formidable/issues/337#issuecomment-1200043198 (node, nextjs) https://github.com/koajs/koa-body/issues/224#issuecomment-1457747782 (vue)

Haven't looking into it, but "webpack5 uses hexoid/dist/index.mjs by default while formidable uses it as a CommonJS module", as quoted by the second link's post author, seems correct.