typescript - 仅使用目录导入 index.ts
问题描述
我正在尝试从目录导入模块 index.ts 而不指定模块名称本身,而仅指定目录名称,这会导致TS2307: Cannot find module
错误。
./src/main.ts:
'use strict';
// success
import {helloWorld as helloWorld1} from '../lib/helloworld/index';
// failure
import {helloWorld as helloWorld3} from '../lib/helloworld';
helloWorld1();
./lib/helloworld/index.ts:
'use strict';
export function helloWorld() {
console.log('Hello World');
}
这在使用原生 JavaScript 时按预期工作,但在 TypeScript 中失败。我究竟做错了什么?
解决方案
确保您使用的是节点路径解析策略。
在您的tsconfig.json
中,将您设置compilerOptions.moduleResolution
为"node"
。
推荐阅读
- sql-server-2017 - 到期余额利息的计算
- google-sheets - 自动计算单元格
- java - 特定模式作为模运算符的输出
- ajax - jquery 自动完成现在工作正常
- oauth-2.0 - 为授权重定向 URI 添加 http://192.168.64.2:8000 域
- python - 有没有办法阻止“unicode escape”错误无法解码字节
- python-3.x - 出现错误 {"detail":"Method \"GET\" not allowed."}
- c - 我想在 UNIX 中制作复制功能文件
- go - 如何在 Golang 中修复“net/http set header ORIGIN”
- sql-server - 如果表中不存在当前记录,如何获取上一个记录