【问题标题】:Typescript 2.0 @Types/Express | Cannot find module打字稿 2.0 @Types/Express |找不到模块
【发布时间】:2017-02-20 09:49:14
【问题描述】:

Typescript 2.0 的 Express 类型定义在运行 ES6 的 npm install @types/expresstsc -t ES6 后执行时会导致意外错误。代码在没有 ES6 标志的情况下编译得很好。谁能解释一下?我也有其他类型定义的问题,例如ssh2

重现步骤如下:

> mkdir humbug
> cd humbug
> touch blank.ts
> tsc blank.ts
> tsc -t ES6 .\blank.ts

到目前为止一切都很好。

> npm init .
...
> npm install @types/express

事情开始变糟了

> tsc blank.ts
> tsc -t ES6 .\blank.ts
node_modules/@types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
node_modules/@types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.

好的,我最好确保已安装 serve-static 和 mime 类型。

> npm install @types/serve-static @types/mime
humbug@1.0.0 C:\Users\me\Desktop\humbug
`-- (empty)

npm WARN humbug@1.0.0 No description
npm WARN humbug@1.0.0 No repository field.
npm ERR! code 1

奇怪的是我得到了一个错误!代码但类型已下载,让我们重新编译一次

> tsc blank.ts
> tsc -t ES6 .\blank.ts
node_modules/@types/express/index.d.ts(16,30): error TS2307: Cannot find module 'serve-static'.
node_modules/@types/serve-static/index.d.ts(16,20): error TS2307: Cannot find module 'mime'.

和上次一样的错误。

我的环境:

  • Windows 10
  • 节点 v6.6.0
  • TSC 2.0.3

存在相同问题的其他定义:

node_modules/@types/cors/index.d.ts(9,26): error TS2307: Cannot find module 'express'.
node_modules/@types/multer/index.d.ts(6,26): error TS2307: Cannot find module 'express'.
node_modules/@types/ssh2/index.d.ts(26,8): error TS2307: Cannot find module 'ssh2-streams'.

【问题讨论】:

    标签: express typescript-typings tsc typescript2.0


    【解决方案1】:

    我仍在学习所有这些东西(作为 .NET 程序员),但今天下午我遇到了类似的问题,而且这个配置似乎正在工作:

    {
        "compilerOptions": {
            "module": "system",
            "noImplicitAny": true,
            "removeComments": true,
            "preserveConstEnums": true,
            "moduleResolution": "node",
            "sourceMap": true,
            "typeRoots": [ "node_modules/@types" ]
        },
        "include": [
            "src/**/*"
        ],
        "exclude": [
            "node_modules",
            "node_modules/@types",
            "**/*.spec.ts"
        ]
    }
    

    【讨论】:

    • 我认为只有 typeRoots 有用,在我的情况下,我需要上一级 "typeRoots": [ "../node_modules/@types" ]
    猜你喜欢
    • 2021-01-07
    • 2013-12-30
    • 2018-06-27
    • 2021-03-16
    • 1970-01-01
    • 1970-01-01
    • 2022-01-06
    相关资源
    最近更新 更多