【发布时间】:2016-09-29 15:29:11
【问题描述】:
我有一个使用 TypeScript 的简单示例项目:https://github.com/unindented/ts-webpack-example
运行 tsc -p .(使用 tsc 版本 1.8.10)会引发以下内容:
app/index.ts(1,21): error TS2307: Cannot find module 'components/counter'.
components/button/index.ts(2,22): error TS2307: Cannot find module 'shared/backbone_base_view'.
components/button/index.ts(3,25): error TS2307: Cannot find module 'shared/backbone_with_default_render'.
components/counter/index.ts(2,22): error TS2307: Cannot find module 'shared/backbone_base_view'.
components/counter/index.ts(3,25): error TS2307: Cannot find module 'shared/backbone_with_default_render'.
components/counter/index.ts(4,27): error TS2307: Cannot find module 'shared/backbone_with_subviews'.
components/counter/index.ts(5,20): error TS2307: Cannot find module 'components/button'.
它抱怨所有本地文件的导入,如下所示:
import Counter from 'components/counter';
如果我将它更改为相对路径,它可以工作,但我不想这样做,因为它让我在移动文件时变得更加困难:
import Counter from '../components/counter';
vscode 代码库不使用相对路径,但对它们来说一切正常,所以我的项目中一定遗漏了一些东西:https://github.com/Microsoft/vscode/blob/0e81224179fbb8f6fda18ca7362d8500a263cfef/src/vs/languages/typescript/common/typescript.ts#L7-L14
您可以查看我的 GitHub 存储库,但如果有帮助,这里是我正在使用的 tsconfig.json 文件:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noImplicitAny": false,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules"
]
}
有趣的是,通过webpack 使用ts-loader 构建项目工作正常,所以我猜这只是一个配置问题...
【问题讨论】:
标签: typescript