javascript - Typescript 2 无法解析外部 Node 模块

标签 javascript node.js typescript typescript2.0

我一直在尝试使用 koa 和 Typescript2.0 进行一些原型(prototype)设计。

我使用以下 tsconfig.json 设置了一个简单的项目:

{
    "compilerOptions": {
        "outDir": "./bin/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "commonjs",
        "target": "es6",
        "jsx": "react",
        "typeRoots": [
            "node_modules/@types"
        ],
        "types": [
            "node"
        ]
    },
    "include": [
        "./src/**/*"
    ]
}

我通过 npm install --save @types/node 安装了 Node 定义文件,现在尝试使用 import Morgan = require('koa-morgan') 导入 Node 模块,但这一直抛出错误:错误 TS7016:找不到模块“koa-morgan”的声明文件。 我正在使用 VScode 并且正确更新了 tsdk,所以这不是问题。

我错过了什么? 提前致谢!

最佳答案

您应该获取 koa-morgan 的类型并以与 node 相同的方式包含它们,或者如果它们不可用并且您有时间并且愿意 - 自己创建一个。

否则只需导入默认的 javascript 方式:

const Morgan = require('koa-morgan');

但这也会让你不需要 typescript 的类型检查

关于javascript - Typescript 2 无法解析外部 Node 模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41466672/

相关文章:

javascript - Angular 2 : AuthGard renders the page before it checks whether SecurityContext is applied or not

javascript - 使用 jQuery/JavaScript 在 Firebase 中嵌套数组

javascript - 如何从随机生成的数组中排除某些值

node.js - 在 32 位系统上编译 node.js

javascript - 有没有办法让一组类方法知道调用何时终止?

angularjs - 找不到 namespace Angular

javascript - 如何通过类名在javascript中动态创建span(已经存在并在html中设置样式,称为span class = "bullet")

javascript - JS Express 连接到 mongoose 数据库

node.js - express .js : Can't redirect after using session middleware

typescript - 如何以字符串作为参数调用Math.round?