引导程序 4 TypeScript 类型定义编译失败 - 找不到模块'popper.js'



我正在尝试设置一个打字稿项目,并使Bootstrap 4与 popper 一起工作在Visual Studio代码中。

我安装了淘汰,jQuery和bootstrap类型定义,

npm install -–save @types/knockout
npm install -–save @types/jquery
npm install --save @types/bootstrap

引用require.js config

中的JS文件
declare var require: any;
require.config({
    paths: {
        "knockout": "externals/knockout-3.5.0",
        "jquery": "externals/jquery-3.3.1.min",
        "popper.js": "https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js",
        "bootstrap": "https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
    }
})

我的tsconfig.json是:

{
    "compilerOptions": {
        "baseUrl": ".",
        "paths": { 
            "*": ["types/*"] 
        },
        "outDir": "./built/",
        "sourceMap": true,
        "noImplicitAny": true,
        "module": "amd",
        "target": "es5"
    },
    "files":[
        "src/require-config.ts",
        "src/hello.ts"
    ]
}

我去编译该项目,然后收到以下错误:

node_modules/@types/bootstrap/index.d.ts:9:25 - error TS2307: Cannot find module 'popper.js'.
9 import * as Popper from "popper.js";
                          ~~~~~~~~~~~
Found 1 error.
The terminal process terminated with exit code: 1

看来我的bootstrap类型定义文件失败了汇编,因为它找不到popper.js模块。

popper.js模块在我的@Types文件夹中

node_modules
 |-@types
 |  |- bootstrap
 |     |-index.d.ts (this is failing)
 |- popper.js
    |- index.d.ts (popper.js type definition)

我如何告诉打字稿编译器,Bootstrap类型定义正在寻找的popper.js位于层次结构中的较高位置?

我甚至还没有找到任何答案。因此,我要么偶然发现了一个没有其他人遇到过的错误(不太可能),要么是新手,我已经不正确地设置了我的环境,并且错过了其他所有人都能获得的非常基本的设置步骤不得不问(很可能)。

我该怎么办?

fan for您:

  • 删除"module": "amd"配置选项或
  • 添加"moduleResolution": "node"选项。

第二个选项看起来像:

"moduleResolution": "node",
"module": "amd",

为什么这些选项有效?

简而言之,当我们使用amd模块时,默认模块分辨率策略为 classic ,而不是 node 。两者之间的重要区别在于,节点策略将目录名称识别为模块,而经典策略则不是。

这里有有关模块分辨率策略的更多信息。

这是我遇到问题时对我产生的第一个结果。

这个github评论对我有帮助。

此库需要DOM库(浏览器环境)

您应该将dom以及'es2017'添加到compileroptions.lib in tsconfig.json。

我仅在我的 tsconfig.json中添加了 "lib": [ "DOM" ],以使其对我有用,而我仍针对es5

最新更新