Node JS throwing error when using Formidable Package

1.5k Views Asked by At

I am following the Node.js Comprehensive Guide textbook.

When attempting to use the "Formidable" package, for uploading forms, I get an error

const form = new formidable.IncomingForm();
                     ^

TypeError: formidable.IncomingForm is not a constructor
    at Server.<anonymous> (file:///C:/Users/mmerc/OneDrive/Programming/Web%20Dev/Node%20JS%20Comprehensive/5/index.js:39:22)
    at Server.emit (node:events:513:28)
    at parserOnIncoming (node:_http_server:1065:12)
    at HTTPParser.parserOnHeadersComplete (node:_http_common:117:17)

Node.js v18.13.0

At the start of the file I use

import formidable from 'formidable';

I tried deleting node_modules, then doing npm install again, but I still get the same error.

I also tried changing the "export" at the end of the formidable index.t.ts file to "exports", as someone recommended that for a single problem, but it made no difference

I'm guessing it could be something to do with the versions of node/formidable, but the book doesn't advise which versions I need to be using.

Does anyone have any ideas? Thanks in advance.

1

There are 1 best solutions below

0
Weiss7777 On

I'm learning nodejs and I'm having the same problem, trying this will solve the problem

import * as formidable from 'formidable';

When formidable is imported this way, it is trying to import a module with a default export. It's possible that formidable doesn't use the default export for ES modules, so such an import won't find a corresponding default export.

import formidable from 'formidable';