TypeScript无法解析外部模块(node.js) [英] TypeScript won't resolve external module (node.js)

查看:355
本文介绍了TypeScript无法解析外部模块(node.js)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的节点应用程序中使用moment.js,所以我使用节点的程序包管理器npm安装了moment.js:

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

为了安全起见,我检查了一下是否未全局安装并且安装的版本实际上是版本2.4.0(版本2.4.0,以便使用正确的d.ts文件...)

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

好的,看起来不错.我也在使用最新版本的TypeScript(0.9.5).

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

因此,现在我将以下文件添加到项目的根目录 https://github.com/borisyankov/DefinitelyTyped/blob/master/moment/moment.d.ts 并引用文件:

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" />

现在,它应该可以使用TypeScripts import关键字导入时刻:

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

import m = require("moment");

使用以下命令进行编译

tsc app.ts --module commonjs

产生以下错误

/home/unknown/temp/test/app.ts(3,1):错误TS2071:无法解决 外部模块时刻". /home/unknown/temp/test/app.ts(3,1):错误 TS2072:模块不能别名为非模块类型.

/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?

推荐答案

d.ts文件中的重要行就是这一行...

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;

这应该使它可以使用您拥有的import语句加载.

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

如果您这样做,则不需要参考注释.

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

这篇关于TypeScript无法解析外部模块(node.js)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆