Search code examples
requirejstypescriptamd

Require external module in Typescript internal module


As the title suggests I have an internal module in which I require an external module (in this case momentjs) If I just have the module definition and reference this in my other files it compiles fine but of course at runtime I get a 'moment is not defined' error. If I then add:

import moment = require('moment');

then the module gets wrapped in the appropriate require([.....]) code but now my other files won't compile due to the module reference not being found.

What is the best way to fix this?


Solution

  • To fix this issue, make sure you have the moment TypeScript definition file included in your project and referenced.

    TypeScript definitions

    You can include moment as follows:

    var moment: moment.MomentStatic = require('moment');