简体   繁体   中英

Typescript 2 cannot resolve external node module

I've been trying to do some prototyping using koa and Typescript2.0.

I've set up a simple project with the following tsconfig.json :

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

I installed the node definition file via npm install --save @types/node and I'm now trying to import a node module with import Morgan = require('koa-morgan') but this keeps throwing the error: error TS7016: Could not find a declaration file for module 'koa-morgan'. I'm using VScode and I properly updated the tsdk so that's not the issue.

What am I missing? Thanks in advance!

You should either get typings for koa-morgan and include them the same way as you did for node , or if they are not available and you have time and will - create one yourself.

Otherwise just import then the defaul javascript way:

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

But that will leave you without type checks from typescript aswell

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM