Search code examples
javascriptangulartypescript

Typescript 'is not a module' error


I have an Angular2 project where i need to import a javascript file for use in my typescript.

I have a javascript file in app/js/d3gantt.js which contains a single function of gantt = function().

gantt = function() {
    //Does lots of stuff
    return gantt;
};

I then have a definition file called d3gannt.d.ts in the same folder which looks like this:

declare module 'd3Gantt' {
    export module d3Gantt {
        export function gantt(): any;
    }
}

And then i reference it in my component as import * as d3Gantt from '../app/js/d3gantt';

However, i get the error message stating File 'c:/Projects/RepackLog/RepackLog/RepackLog/app/js/d3gantt.d.ts' is not a module

Am i missing something that is needed for this to pick up my files properly?

Thanks,


Solution

  • As @FabioAntunes mentioned in one of the comments above, you just have to export gantt on your d3gantt.js file itself, in-line. The exact syntax would be

    export gantt = function() {
        //Does lots of stuff
        return gantt;
    };
    

    No need to declare it anywhere else. For further note on exporting modules please refer this post (Typescript es6 import module "File is not a module error").