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.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

ugobey opened this issue · comments

Support plan

  • Which support plan is this issue covered by? (Community, Sponsor, Enterprise): Community
  • Currently blocking your project/work? (yes/no): yes
  • Affecting a production system? (yes/no): yes

Context

  • Node.js version: v18.16.0
  • Release Line of Formidable (Legacy, Current, Next): Current
  • Formidable exact version: 3.5.1
  • Environment (node, browser, native, OS): node
  • Used with (popular names of modules): express

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

Unbelievable

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` });

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

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

Same problem here

That solved it for me thanks everyone!

Is everyone having this issue using Typescript ?

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

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

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.

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?