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
7k stars 680 forks source link

Since V3 I get "formidable is not a function" - using require not import #955

Closed ugobey closed 6 months ago

ugobey commented 9 months ago

Support plan

Context

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

I simply upgraded to V3 and now get an error of "formidable is not a function". Im using const formidable = require("formidable"); not import as I am not using a module and to change it is a huge hassle as it affects loads of other things.

What was the result you got?

"formidable is not a function"

What result did you expect?

for it to work like it did in V2

GrosSacASac commented 9 months ago

Unbelievable

GrosSacASac commented 9 months ago

https://npmfs.com/package/formidable/3.5.1/dist/index.cjs#L1682 the exported default is a function

ugobey commented 9 months ago

And yet when I revert to V2 it works and simply upgrade to V3 again it fails with same error which makes no sense I agree but its happening

const formidable = require("formidable");

app.post("/upload", (req, res, next) => {
        const form = formidable({ multiples: true, uploadDir: `${__dirname}/upload` });

        form.parse(req, (err, fields, files) => {
            if (err) {
                next(err);
                return;
            }

            var file = files.file.filepath;
            var mimetype = files.file.mimetype;

            if (mimetype === "image/png" || mimetype === "image/jpeg") {
                Jimp.read(file)
                    .then(async (logo) => {
                        await logo.resize(48, 48).quality(100).writeAsync(`${__dirname}/output/icon.png`);

                        var getNewFile = fs.readFileSync(`${__dirname}/output/icon.png`, "base64");

                        fs.unlinkSync(file);
                        fs.unlinkSync(`${__dirname}/output/icon.png`);

                        res.end(getNewFile);
                    })
                    .catch((err) => {
                        errorHandler("Error : Jimp : " + err);
                    });
            } else {
                fs.unlinkSync(file);
                res.end("WRONG MIMETYPE");
            }
        });
    });

It fails at line

const form = formidable({ multiples: true, uploadDir: `${__dirname}/upload` });
Sumzibuyu commented 8 months ago

Using the following command to import can solve the problem, and I don't know why const {formidable} = require("formidable");

crowmw commented 8 months ago

Have the same problem. const {formidable} = require("formidable"); helped me too, i think it mey be problem with @types/formidable

RoseannePeeters commented 6 months ago

Same problem here

ugobey commented 6 months ago

That solved it for me thanks everyone!

GrosSacASac commented 6 months ago

Is everyone having this issue using Typescript ?

RoseannePeeters commented 6 months ago

Is everyone having this issue using Typescript ?

No I am not

This works though:

Have the same problem. const {formidable} = require("formidable"); helped me too, i think it mey be problem with @types/formidable

ugobey commented 6 months ago

Im not using Typescript and have that issue but solved from the suggestion above

tunnckoCore commented 3 months ago

That's normal. It's a SemVer Major release, and it's documented. We tried to have backwards compat but yeah.

We have exports.default, but i think we should also have module.exports for all that to work "properly".. in general, i think we can safely drop CJS for v4.

Gin49SZ commented 2 months ago

Using the following command to import can solve the problem, and I don't know why const {formidable} = require("formidable");

I meet the same problem, does it affect any subsequent code?