Question

I would like to use moment.js in my node application, so I installed moment.js using node's package manager npm:

npm install moment@2.4.0

Just to be on the safe side, I checked moment is not installed globally and the installed version is really version 2.4.0 (version 2.4.0 in order to use the correct d.ts file ...)

require("moment").version

Alright, seems to be good. I'm also using the latest version of TypeScript (0.9.5).

So, now I added the following file to my projects root directory https://github.com/borisyankov/DefinitelyTyped/blob/master/moment/moment.d.ts and refernced the file:

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

Now, it should work to import moment using TypeScripts import keyword:

import m = require("moment");

Compiling with the following command

tsc app.ts --module commonjs

produces the following errors

/home/unknown/temp/test/app.ts(3,1): error TS2071: Unable to resolve external module '"moment"'. /home/unknown/temp/test/app.ts(3,1): error TS2072: Module cannot be aliased to a non-module type.

Why does this error occur? How do I fix it?

Was it helpful?

Solution

The important line in the d.ts file is this one...

declare var moment: MomentStatic;

It just declares a variable for moment.

You can add the following line to resolve your problem:

export = moment;

This should make it loadable using the import statement you have.

If you do this - you won't need yhe reference comment.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top