1

I have a NodeJS + ExpressJS which I am migrating to Typescript. I am using a Node module "Formidable" for which I am trying to create a typescript definition. However, I'm not able to import the module elements into Typescript code due to an error:

Here;s the module definition:

/// <reference path="../Main.d.ts" />

declare module "formidable" {
    export class Formidable {
        constructor();
        IncomingForm() : Form;
    }

    export class Form{
        encoding : String;
        uploadDir : String;
        keepExtensions : Boolean;
        type : String;
        parse(String, Function) : void;
    }

}

Here's the file "Upload.ts" that imports the module

import fs = require('fs');
import path = require('path');
import formidableModule = require('formidable');
var formidable = new formidableModule.Formidable();

Here's "Upload.js" created by the Typescript compiler

var fs = require('fs');
var path = require('path');
var formidableModule = require('formidable');
var formidable = new formidableModule.Formidable();

Here's the error

C:\Users\Me\WebstormProjects\Core\lib\Upload.js:5
var formidable = new formidableModule.Formidable();
                 ^
TypeError: undefined is not a function
    at Object.<anonymous> (C:\Users\Anjan\WebstormProjects\Core\lib\Upload.js:5:18)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (C:\Users\Anjan\WebstormProjects\Core\app.js:9:20)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)

Process finished with exit code 8

2 Answers 2

0

Not sure about ts but this is how I export entire classes (I had this same problem)

module.exports.formidable = {
    method1: function(req){
        /// code here
    },
    method2: function(param1, param2) {
            ///code here
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is the way to do it in NodeJS without TS - which works fine with me any ways. However, because I'm migrating to TS, I'm trying to figure how to do it in TS.
0

I think your definition for Formidible is incorrect.

Basically you are getting undefined on Formidable class I don't see that in the docs https://github.com/felixge/node-formidable#api

Perhaps https://github.com/felixge/node-formidable#formidableincomingform :

var form = new formidable.IncomingForm()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.