Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
ugobey opened this issue Sep 28, 2023 · 12 comments
Closed
Labels

Comments

@ugobey
Copy link

ugobey commented Sep 28, 2023

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

@ugobey ugobey added the bug label Sep 28, 2023
@GrosSacASac
Copy link
Contributor

GrosSacASac commented Sep 28, 2023

Unbelievable

@GrosSacASac
Copy link
Contributor

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

@ugobey
Copy link
Author

ugobey commented Sep 28, 2023

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
Copy link

Sumzibuyu commented Oct 27, 2023

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

@crowmw
Copy link

crowmw commented Nov 2, 2023

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

@RoseannePeeters
Copy link

Same problem here

@ugobey
Copy link
Author

ugobey commented Dec 21, 2023

That solved it for me thanks everyone!

@ugobey ugobey closed this as completed Dec 21, 2023
@GrosSacASac
Copy link
Contributor

Is everyone having this issue using Typescript ?

@RoseannePeeters
Copy link

RoseannePeeters commented Dec 23, 2023

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
Copy link
Author

ugobey commented Dec 25, 2023

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

@tunnckoCore
Copy link
Member

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
Copy link

Gin49SZ commented Apr 22, 2024

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

7 participants