tsc 抛出“TS2307:无法找到本地文件的模块" [英] tsc throws `TS2307: Cannot find module` for a local file
问题描述
我有一个使用 TypeScript 的简单示例项目:https://github.com/unindented/ts-webpack-example
I've got a simple example project using TypeScript: https://github.com/unindented/ts-webpack-example
运行 tsc -p .
(使用 tsc
1.8.10 版)会抛出以下内容:
Running tsc -p .
(with tsc
version 1.8.10) throws the following:
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'.
它抱怨所有本地文件的导入,如下所示:
It complains about all imports of local files, like the following:
import Counter from 'components/counter';
如果我将其更改为相对路径,它会起作用,但我不想这样做,因为在移动文件时这会使我的生活变得更加困难:
If I change it to a relative path it works, but I don't want to, as it makes my life more difficult when moving files around:
import Counter from '../components/counter';
vscode
代码库不使用相对路径,但对它们来说一切正常,所以我的项目中一定缺少一些东西:https://github.com/Microsoft/vscode/blob2816fc6f2fcf2f9f2f9f2f9f2f9f2f9f2f9f2f9f2f9f2f9f2f9fcf9fcfc/languages/typescript/common/typescript.ts#L7-L14
The vscode
codebase does not use relative paths, but everything works fine for them, so I must be missing something in my project: https://github.com/Microsoft/vscode/blob/0e81224179fbb8f6fda18ca7362d8500a263cfef/src/vs/languages/typescript/common/typescript.ts#L7-L14
您可以查看我的 GitHub 存储库,但如果它有帮助,这里是我正在使用的 tsconfig.json
文件:
You can check out my GitHub repo, but in case it helps here's the tsconfig.json
file I'm using:
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"noImplicitAny": false,
"removeComments": false,
"preserveConstEnums": true,
"sourceMap": true,
"outDir": "dist"
},
"exclude": [
"dist",
"node_modules"
]
}
有趣的是,使用 ts-loader
通过 webpack
构建项目工作正常,所以我猜这只是一个配置问题...
Funny thing is, building the project through webpack
using ts-loader
works fine, so I'm guessing it's just a configuration issue...
推荐答案
@vladima 回复 GitHub 上的这个问题:
@vladima replied to this issue on GitHub:
编译器解析模块的方式由moduleResolution 选项可以是 node
或 classic
(更多可以在此处找到详细信息和差异.如果省略此设置编译器将此设置视为 node
如果模块是 commonjs
并且经典
- 否则.在你的情况下,如果你想要 classic
模块与 commonjs
模块一起使用的解析策略 - 您需要设置它明确地使用
The way the compiler resolves modules is controlled by moduleResolution option that can be either
node
orclassic
(more details and differences can be found here). If this setting is omitted the compiler treats this setting to benode
if module iscommonjs
andclassic
- otherwise. In your case if you wantclassic
module resolution strategy to be used withcommonjs
modules - you need to set it explicitly by using
{
"compilerOptions": {
"moduleResolution": "node"
}
}
这篇关于tsc 抛出“TS2307:无法找到本地文件的模块"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!