我有一个使用 TypeScript 的简单示例项目:https://github.com/unindented/ts-webpack-example
Running tsc -p .
(with 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';
The 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
using ts-loader
工作正常,所以我猜这只是一个配置问题......