born2net born2net - 28 days ago 7
TypeScript Question

TypeScript and ng2 rc.1 getting Error:(20, 15) TS2304: Cannot find name 'module'

TypeScript and ng2 rc.1 getting Error:(20, 15) TS2304: Cannot find name 'module'.

when trying to use directive of module as in

@Component({
selector: 'Notes1',
moduleId: module.id,
directives: [ModalDialog, FORM_DIRECTIVES, DisplayError],
templateUrl: 'Notes1.html',
styleUrls: ['Notes1.css']
})


any idea how to fix the TS error.. all is working fine at runtime

regards

Sean

Answer

The error means TypeScript compiler doesn't know what module is. For a quick fix:

declare var require: any;

To make it more complete, use a definition from DefinitelyTyped/node:

interface NodeRequireFunction {
    (id: string): any;
}

interface NodeRequire extends NodeRequireFunction {
    resolve(id:string): string;
    cache: any;
    extensions: any;
    main: any;
}

declare var require: NodeRequire;

interface NodeModule {
    exports: any;
    require: NodeRequireFunction;
    id: string;
    filename: string;
    loaded: boolean;
    parent: any;
    children: any[];
}

declare var module: NodeModule;