TypeScript will not allow an external module (node.js)

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

npm install moment@2.4.0 

Just to be safe, I checked a point that is not installed globally, and the installed version is actually version 2.4.0 (version 2.4.0, to use the correct d.ts file ...)

 require("moment").version 

Good, seems good. I also use the latest version of TypeScript (0.9.5).

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

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

Now it should work to import the moment using the import TypeScripts 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: it is not possible to solve the external module "moment". /home/unknown/temp/test/app.ts(3,1): error TS2072: the module cannot be an alias for a non-modular type.

Why does this error occur? How to fix it?

+7
commonjs typescript momentjs
source share
1 answer

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

 declare var moment: MomentStatic; 

It simply declares a variable for the moment.

You can add the following line to solve your problem:

 export = moment; 

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

If you do this, you will not need a reference comment.

+6
source share

All Articles