【发布时间】:2017-11-09 20:20:28
【问题描述】:
我想像这样导入我的模块,而不是相对模块导入:import { IntHelper } from 'utils/IntHelper';。即使智能感知在 VSCode 中运行良好,转译的 javascript 文件也会引发异常:Cannot find module。
我的项目结构:
根
- 分布
- 源
- MyProject.ts
- 实用程序
- IntHelper.ts
- tsconfig.json
文件:MyProject.ts
import { IntHelper } from 'utils/IntHelper';
文件:IntHelper.ts
export module IntHelper {
export const xy: string = 'Test';
export function crossSum(int: number) {
return int; // Nonsense - ofcourse.
}
}
Tsconfig.json:
{
"compilerOptions": {
"module": "commonjs",
"target": "es6",
"noImplicitAny": true,
"moduleResolution": "node",
"sourceMap": true,
"outDir": "dist",
"baseUrl": ".",
"paths": {
"*": [
"*",
"src/*"
]
}
}
}
我的问题:
为什么它会在 javascript 文件中抛出找不到模块异常,即使它在 typescript 文件中看起来很好?当我将'utils/IntHelper' 部分悬停在打字稿文件的导入行中时,VSCode 也会显示该模块的正确路径。
【问题讨论】:
-
你的建造者是什么?
webpack,fusebox,还是直接使用tsc? -
您可以向 typescript 编译器提供
--traceResolution以查看您的模块未解析的原因。如果它被编译器解决,您的问题可能来自您的构建解决方案。 -
试试
import { IntHelper } from './utils/IntHelper';
标签: typescript tsconfig