Using moment.js with typescript and “module”: “amd”

北城余情 提交于 2019-12-11 06:25:28

问题


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:


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.



来源:https://stackoverflow.com/questions/41694525/using-moment-js-with-typescript-and-module-amd

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!