typescript - Bootstrap4 typings(index.d.ts) 在 VisualStudio 2019 上使用 TypeScript3.9 和 Libman 找不到模块“popper.js”
问题描述
我将 VisualStudio 2019 与 TypeScript3.9 和 Libman 一起使用。
我需要 Bootstrap4 和 jQuery。因此,请尝试通过 Libman 获取这些库和类型(index.d.ts)。
然后 Bootstrap4 键入(index.d.ts)得到错误“找不到模块 popper.js”。
// Type definitions for Bootstrap 4.5
// Project: https://github.com/twbs/bootstrap/, https://getbootstrap.com
// Definitions by: denisname <https://github.com/denisname>
// Piotr Błażejewicz <https://github.com/peterblazejewicz>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.3
/// <reference types="jquery"/>
import * as Popper from "popper.js"; // ERROR!: "Cannot find module popper.js"
我发现了类似的问题和答案。
- Bootstrap 4 TypeScript 类型定义无法编译 - 找不到模块'popper.js'
- 找不到模块“popper.js”Angular 5 Visual Studio 2017 asp.net 核心
- Angular @types/bootstrap 错误:命名空间“popper.js”没有导出成员
只能暂时解决问题。
我将 bootstrap4 typing(index.d.ts) 更改为相对路径,这import * as Popper from "../popper_v1";
是固定错误。但是 Libman 会覆盖这个手动更改的 index.d.ts。
_
我能做些什么来解决这个错误?我是否需要安装手动修改的 index.d.ts,比如 Popper v1.X?
谢谢。
重现错误的步骤。
在 .Net Framework 4.8 上创建 ASP.Net4 MVC 项目
创建 libman.json 以添加一些库和 TS 类型
{
"version": "1.0",
"defaultProvider": "jsDelivr",
"libraries": [
{
"library": "bootstrap@4.5.2",
"destination": "lib/bootstrap/"
},
{
"library": "@types/bootstrap@4.5.0",
"destination": "lib/types/bootstrap/"
},
{
"library": "jquery@3.5.1",
"destination": "lib/jquery/",
"files": [
"dist/jquery.js",
"dist/jquery.min.js",
"dist/jquery.min.map",
"dist/jquery.slim.js",
"dist/jquery.slim.min.js",
"dist/jquery.slim.min.map",
"external/sizzle/LICENSE.txt",
"external/sizzle/dist/sizzle.js",
"external/sizzle/dist/sizzle.min.js",
"external/sizzle/dist/sizzle.min.map",
"AUTHORS.txt",
"LICENSE.txt",
"README.md"
]
},
{
"library": "@types/jquery@3.5.1",
"destination": "lib/types/jquery/"
},
{
"library": "@types/sizzle@2.3.2",
"destination": "lib/types/sizzle/"
},
{
"provider": "cdnjs",
"library": "popper.js@1.16.1",
"destination": "lib/popper.js/"
},
{
"provider": "filesystem",
"library": "lib_ManualInstallSources/popper_v1/",
"destination": "lib/types/popper_v1/"
}
]
}
** 注意:我从 GitHub 存储库中获得了 popper.js ver.1.X 的类型(index.d.ts)。
- 创建 tsconfig.json
{
"compilerOptions": {
"noImplicitAny": false,
"noEmitOnError": true,
"removeComments": false,
"sourceMap": true,
"target": "es5",
"lib": [ "ES6", "DOM" ],
"baseUrl": ".",
"typeRoots": [
"./lib/types"
]
},
"exclude": [
"node_modules",
"wwwroot",
"lib"
]
}
完成这些步骤后,bootstrap4 typing(index.d.ts) 得到错误“找不到模块 popper.js” import * as Popper from "popper.js";
。
解决方案
我找到了两个要修复的petterns。
- 将 tsconfig.json 设置更改为编译器可以搜索“lib”目录。
- 通过 Libman 将库的目录名称更改为“node_modules”(不推荐)。
这个问题是由多种原因引起的。
- TypeScript 的默认模块管理机制基于 node.js。
- Libman 默认使用“lib”目录来放置库。
- 库的目录名称必须与 ModuleName 相同。
[原因]
1、TypeScript的模块管理机制基于node.js。
我的 tsconfig.json 有"target": "ES5"
. 并且没有"module"
财产。
它使"module"
属性默认。在"ES5"
目标下,默认为"CommonJS"
.
那么,也没有“moduleResolution”属性。所以"module": "CommonJS"
使"moduleResolution"
属性为"Node"
。
是的,编译器使用“节点”模块解析系统。
.
> TypeScript 3.9 上的“节点”解析系统
这本手册说,非相对导入(例如import * from "banana"
)将递归搜索“node_modules”目录(到父级)。
是的,目录必须命名为“node_modules”。
否则我们使用 Libman,但最新的 TypeSciript 的基本理论是基于“node.js”!
而“经典”解析系统是遗留的且无用的。
最后,我们需要更改 tsconfig.json 以搜索我们的“lib”目录。或者将目录名称更改为“node_modules”以适应“节点”解析系统(不推荐。我们使用 Libman,而不是节点)。
2. Libman 默认使用“lib”目录放置库。
作为 1 的原因,我们需要“node_modules”目录。但是 libman 的默认目录名称是“lib”。
这导致编译器无法在“lib”目录中搜索库。
3. 库目录名必须与ModuleName 相同。
如果使用非相对导入(例如import * from "banana"
),编译器搜索“./node_modules/banana.js(ts)”或“./node_modules/banana/*”。
我的 libman.config 具有 popper.js 版本 1.X 的类型(.d.ts)。
{
"provider": "filesystem",
"library": "lib_ManualInstallSources/popper_v1/",
"destination": "lib/types/popper_v1/"
}
是的......我的"desitination"
财产是“popper_v1”。
这是错误的命名。
import * as Popper from "popper.js"
找不到这个目录。需要将目录名称修复为“lib/types/popper.js”。
[怎么修]
我找到了两个要修复的petterns。
- 将设置 tsconfig.json 更改为编译器可以搜索“lib”目录。
- 通过 Libman 将库的目录名称更改为“node_modules”(不推荐)。
我建议使用 1。因为我们使用的是 Libman,而不是 Node。如果我们看到“node_modules”目录,就会导致我们误以为我们在使用 Node.js。
但是,通过更改为“node_modules”,可以使整体配置看起来更容易。
1.更改设置tsconfig.json为编译器可以搜索“lib”目录。
// tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"target": "es5",
"lib": [ "ES6", "DOM" ],
"baseUrl": ".",
"typeRoots": [
"./node_modules/@types", // Default path to search typings.
"./lib/types" // Directory of typings that located by Libman.
],
"paths": {
// If absolute module name provided on .ts/.d.ts files, compiler will check "./lib/moduleName", "./lib/types/moduleName".
// e.g. 'import * as Popper from "popper.js"' is convert to "./lib/popper.js" and "./lib/types/popper.js".
// Then "./lib/types/popper.js" is correctly path. Error fixed.
"*": [ "lib/*", "lib/types/*" ]
}
},
"exclude": [
"node_modules",
"wwwroot",
"lib"
]
}
// libman.json
{
"version": "1.0",
"defaultProvider": "jsDelivr",
"libraries": [
{
"provider": "cdnjs",
"library": "popper.js@1.16.1",
"destination": "lib/popper.js/"
},
{
"provider": "filesystem",
"library": "lib_ManualInstallSources/popper_v1/",
"destination": "lib/types/popper.js/"
},
{
"library": "bootstrap@4.5.2",
"destination": "lib/bootstrap/"
},
{
"library": "@types/bootstrap@4.5.0",
"destination": "lib/types/bootstrap/"
},
{
"library": "jquery@3.5.1",
"destination": "lib/jquery/",
"files": [
"dist/jquery.js",
"dist/jquery.min.js",
"dist/jquery.min.map",
"dist/jquery.slim.js",
"dist/jquery.slim.min.js",
"dist/jquery.slim.min.map",
"external/sizzle/LICENSE.txt",
"external/sizzle/dist/sizzle.js",
"external/sizzle/dist/sizzle.min.js",
"external/sizzle/dist/sizzle.min.map",
"AUTHORS.txt",
"LICENSE.txt",
"README.md"
]
},
{
"library": "@types/jquery@3.5.1",
"destination": "lib/types/jquery/"
},
{
"library": "@types/sizzle@2.3.2",
"destination": "lib/types/sizzle/"
}
]
}
2. 通过 Libman 将库的目录名称更改为“node_modules”(不推荐)。
{
"compilerOptions": {
"sourceMap": true,
"target": "es5",
"lib": [ "ES6", "DOM" ],
"baseUrl": "."
},
"exclude": [
"node_modules",
"wwwroot",
"lib"
]
}
{
"version": "1.0",
"defaultProvider": "jsDelivr",
"libraries": [
{
"provider": "cdnjs",
"library": "popper.js@1.16.1",
"destination": "node_modules/popper.js/"
},
{
"provider": "filesystem",
"library": "lib_ManualInstallSources/popper_v1/",
"destination": "node_modules/types/popper.js/"
},
{
"library": "bootstrap@4.5.2",
"destination": "node_modules/bootstrap/"
},
{
"library": "@types/bootstrap@4.5.0",
"destination": "node_modules/types/bootstrap/"
},
{
"library": "jquery@3.5.1",
"destination": "node_modules/jquery/",
"files": [
"dist/jquery.js",
"dist/jquery.min.js",
"dist/jquery.min.map",
"dist/jquery.slim.js",
"dist/jquery.slim.min.js",
"dist/jquery.slim.min.map",
"external/sizzle/LICENSE.txt",
"external/sizzle/dist/sizzle.js",
"external/sizzle/dist/sizzle.min.js",
"external/sizzle/dist/sizzle.min.map",
"AUTHORS.txt",
"LICENSE.txt",
"README.md"
]
},
{
"library": "@types/jquery@3.5.1",
"destination": "node_modules/types/jquery/"
},
{
"library": "@types/sizzle@2.3.2",
"destination": "node_modules/types/sizzle/"
}
]
}
推荐阅读
- r - 数据框的 R 嵌套/未嵌套会导致不同的对象
- javascript - Javascript突出显示切换开关上的差异
- r - 如何匹配仅包含任意顺序和任意数字的字符集的字符串?
- html - 背景图像未在css中显示
- java - Android Scroll View 与它下面的其他 View 重叠
- javascript - 安装 dojo/cli 时缺少写入权限(尽量避免使用 sudo)
- python - 删除字典中的空值
- serial-port - Raspberry Pi 4 上 Ubuntu 20.04 上的 GPS 串行控制台问题
- javascript - 如何为 html 和 Js 游戏做倾斜控制
- regex - MongoDB“查找”-如何查询包含字符串、不区分大小写和区分变音符号的字段的文档?