1
votes

I'm trying to use moment.js from typescript 2.1.5

I installed moment with npm :

npm install moment --save-dev

The d.ts file is included with moment.js so no install via @typings is required but when I compile my project I get the following error :

error TS2307: Cannot find module 'moment'.

Here is a simple test I made to repro the issue.

repro.ts file

import * as moment from "moment";
const date = moment().format("YYYY");
console.log(date);

tsconfig.json file :

{
    "compilerOptions": {
        "module": "amd"
    }
}

If i compile with :

.\node_modules\.bin\tsc

I get the error. I notice that the compilation is fine if I target commonjs module ( "module": "commonjs" in tsconfig )

What is the correct way of using moment if I target amd module ?

1

1 Answers

2
votes

You have to add "moduleResolution": "node" to compilerOptions in tsconfig.json.

When omitted, moduleResolution defaults to classic unless module is commonjs, that's the reason your modules are not found in node_modules.

Also, it looks like this is going to be fixed in some future release of the compiler.